创建一个字段,其值是其他字段值的计算 [英] Create a field whose value is a calculation of other fields' values

查看:111
本文介绍了创建一个字段,其值是其他字段值的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pre $ class PO(models.Model)
qty = models.IntegerField(null = True)
cost = models.IntegerField(null = True)
total = qty * cost

如何解决 total = qty * cost 以上。我知道这会导致错误,但不知道如何处理这个问题。

解决方案

Justin Hamades回答

  class PO(models.Model)
qty = models.IntegerField(null = True)
cost = models.IntegerField(null = True)

@property
def total(self):
return self.qty * self.cost


class PO(models.Model)
    qty = models.IntegerField(null=True)
    cost = models.IntegerField(null=True)
    total = qty * cost

How will I solve total = qty * cost above. I know it will cause an error, but have no idea of how to deal with this.

解决方案

Justin Hamades answer

class PO(models.Model)
    qty = models.IntegerField(null=True)
    cost = models.IntegerField(null=True)

    @property
    def total(self):
        return self.qty * self.cost

这篇关于创建一个字段,其值是其他字段值的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆