2012-07-13 92 views

回答

12
+1

@ ahsan的回答有什麼問題,它正是你需要的嗎? – Ahsan 2012-07-19 04:53:30

+0

這是錯誤的,因爲總數不是屬性,而是一種方法。 – datashaman 2013-01-22 17:51:43

+3

@MarlinForbes是不是'total = property(_get_total)與'@ property'完全相同?檢查[這裏](http://www.artima.com/weblogs/viewpost.jsp?thread=240808)。 – agconti 2013-08-22 15:38:30

35

您可以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) 
+0

有什麼辦法我們可以把它作爲數量和成本的計算? – rechie 2012-07-13 06:40:02

+0

'total'將表現得像一個字段,您可以像其他字段一樣通過類對象訪問它。 – Ahsan 2012-07-13 06:46:25

+0

@Secator ..謝謝! – rechie 2012-07-13 07:33:53