python - 如何创建包含两个不同查询集之和的表

标签 python python-3.x django django-models django-views

我的 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/

相关文章:

python - 我怎样才能让 Python 找到 ffprobe?

python - Pandas : Map the list value in a column to dictionary based on group

python - 如何在 python 中的多个函数上使用相同的装饰器?

python - 在 Mac 上使用 VirtualEnv : Can't find interpreter 安装 OpenCV Python 绑定(bind)

python - Appium 在 iOS(真实设备)上访问 URL 会遇到 "(' Connection aborted .', BadStatusLine("''",))"

python-3.x - 禁用部分 nlp 管道

python - 我怎样才能不出现 mysql 转换元组错误?

python - 在 Django 模板中列出目录文件内容

javascript - 如何获取 django View 的 url 字符串?

python - 如何在 Tensorflow (python) 中随机选取和屏蔽张量的一部分