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