Create a field whose value is a calculation of other fields' values

前端 未结 2 1562
长发绾君心
长发绾君心 2020-12-14 18:11
class PO(models.Model)
    qty = models.IntegerField(null=True)
    cost = models.IntegerField(null=True)
    total = qty * cost

How will I solve <

2条回答
  •  臣服心动
    2020-12-14 18:40

    You can make total a property field, see the 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)
    

提交回复
热议问题