我在文件(services.py)中有一个函数:
def balance(revenue, expenses):
bottom_line = revenue - expenses
return bottom_line
收入应用程序内的收入模型:
class Revenue(model.Models):
revenue = model.DecimalField()
费用APP中的费用模型,我想在保存时计算并填充“bottom_line”字段:
class Expenses(model.Models):
expenses = model.DecimalField()
bottom_line = model.DecimalField()
def save(self, *args, **kwargs):
bottom_line = balance(revenue, self.expenses)
super(Expenses, self).save(*args, **kwargs)
如何在上述模型的保存覆盖期间访问revenue属性?我是否需要执行查询集以从Expenses模型中获取bottom_line属性?