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)
total = qty * cost
我将如何解决上面的total = qty * cost
。我知道这会导致错误,但不知道如何处理这个问题。创建一个字段,其值是其他字段值的计算
您可以total
一个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)
@ ahsan的回答有什么问题,它正是你需要的吗? – Ahsan 2012-07-19 04:53:30
这是错误的,因为总数不是属性,而是一种方法。 – datashaman 2013-01-22 17:51:43
@MarlinForbes是不是'total = property(_get_total)与'@ property'完全相同?检查[这里](http://www.artima.com/weblogs/viewpost.jsp?thread=240808)。 – agconti 2013-08-22 15:38:30