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