2012-07-13 6 views

答えて

12
+1

@ ahsanの答えが間違っています、それはあなたが必要とするものです。 – Ahsan

+0

これは微妙に間違っています。合計はプロパティではないので、メソッドです。 – datashaman

+3

@MarlinForbes 'total = property(_get_total)same'を' @ property'にisntしますか? [ここ](http://www.artima.com/weblogs/viewpost.jsp?thread=240808)をチェックしてください。 – agconti

35

あなたはtotalproperty場を作ることができ、参照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

+0

'total'はフィールドのように動作し、他のフィールドと同様にクラスオブジェクトを通じてアクセスできます。 – Ahsan

+0

@Secator ..ありがとう! – rechie