Ответ 1
class PO(models.Model)
qty = models.IntegerField(null=True)
cost = models.IntegerField(null=True)
@property
def total(self):
return self.qty * self.cost
class PO(models.Model)
qty = models.IntegerField(null=True)
cost = models.IntegerField(null=True)
total = qty * cost
Как я могу решить total = qty * cost
выше. Я знаю, что это вызовет ошибку, но не имеет понятия, как с этим справиться.
class PO(models.Model)
qty = models.IntegerField(null=True)
cost = models.IntegerField(null=True)
@property
def total(self):
return self.qty * self.cost
Вы можете сделать поле total
a property
, см. docs
class PO(models.Model)
qty = models.IntegerField(null=True)
cost = models.IntegerField(null=True)
def _get_total(self):
"Returns the total"
return self.qty * self.cost
total = property(_get_total)