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
これは微妙に間違っています。合計はプロパティではないので、メソッドです。 – datashaman
@MarlinForbes 'total = property(_get_total)same'を' @ property'にisntしますか? [ここ](http://www.artima.com/weblogs/viewpost.jsp?thread=240808)をチェックしてください。 – agconti