我的 django 应用程序中有以下模型:
class Budget_Vendite(models.Model):
product=models.ForeignKey()
byproduct=models.ForeignKey()
quant_jan=models.DecimalField()
quant_feb=models.DecimalField()
quant_mar=models.DecimalField()
price_jan=models.DecimalField()
price_feb=models.DecimalField()
price_mar=models.DecimalField()
我想创建一个数据集,为每个副产品计算出 quant_jan*price_jan
给出的每月金额
广告示例:
byproduct | jan | feb |
byproduct_1 | quant_jan*price_jan | quant_feb*price_feb |
最佳答案
在模型中定义方法,每个方法对应每月,例如:
def january(self):
return self.quant_jan * self.price_jan
.
.
.
def march(self):
return self.quant_mar * self.price_mar
并在相应列的模板 html 中调用它们
每月金额:
def year_sum(self):
months = ((quant_jan, price_jan), ...)
sum = 0
for month in months:
sum += getattr(self, month[0]) * getattr(self, month[1])
return sum
关于python - 如何创建包含两个不同查询集之和的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62576229/