2024-04-19 15:02:35 发布
网友
class PO(models.Model) qty = models.IntegerField(null=True) cost = models.IntegerField(null=True) total = qty * cost
我将如何解决上面的total = qty * cost。我知道这会导致错误,但不知道如何处理。
total = qty * cost
您可以将total设为property字段,请参见docs
total
property
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)
Justin Hamades answer
class PO(models.Model) qty = models.IntegerField(null=True) cost = models.IntegerField(null=True) @property def total(self): return self.qty * self.cost
您可以将
total
设为property
字段,请参见docsJustin Hamades answer
相关问题 更多 >
编程相关推荐