0
說這是我簡單的models.py
如何將自定義函數綁定到模型字段?
class Order(models.Model):
quantity = models.IntegerField()
item_price = models.FloatField()
我想有一個函數來計算總價:
def calc_total(self):
return self.quantity * self.item_price
現在,我該如何創建示範田total_price
所以它被自動填充在數據庫中?我知道我可以在模板中使用calc_total
方法,但是我也需要這個方法。
定義'total_price' = models.IntegerField(),然後在訂貨型號的實例讓我們說'order_instance'調用方法'order_instance.cal_total()' in'calc_total():' 'self.total = self.quantity * self.item_price' – rsudip90