2012-07-13 82 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

相关问题