Имею модель Order, Discount и Tax.
Выглядят вот так
class Discount(models.Model):
order = models.ForeignKey('Order', on_delete=models.CASCADE, related_name='discounts')
name = models.CharField(max_length=100)
amount = models.IntegerField()
def __str__(self):
return self.name
class Tax(models.Model):
order = models.ForeignKey('Order', on_delete=models.CASCADE, related_name='taxes')
name = models.CharField(max_length=100)
rate = models.IntegerField()
def __str__(self):
return self.name
class Order(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
address = models.CharField(max_length=250)
postal_code = models.CharField(max_length=20)
city = models.CharField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
paid = models.BooleanField(default=False)
items = models.ManyToManyField(Item, through='OrderItem')
discount = models.ForeignKey(Discount, null=True, blank=True, on_delete=models.SET_NULL, related_name='order_discount')
tax = models.ForeignKey(Tax, null=True, blank=True, on_delete=models.SET_NULL, related_name='order_tax')
class Meta:
ordering = ('-created_at',)
def __str__(self):
return f'Order {self.id} | {self.created_at.strftime("%d.%m.%Y %H:%M")}'
def get_total_cost(self):
total = sum(item.price for item in self.items.all())
if self.discount:
total -= self.discount.amount
if self.tax:
tax_amount = total * self.tax.rate / 100
total += tax_amount
return total
Скидку и налог ввёл через админку, но почему-то в расчётах не учитываются. А когда хотел принтануть вывел
print(total, self.discount.amount, self.tax.rate)
^^^^^^^^^^^^^^^^^^^^
AttributeError: ‘NoneType’ object has no attribute ‘amount’
Подскажите причину