首页 > 解决方案 > 注释功能给我一个错误的结果

问题描述

我在我的 django 应用程序中设置了以下三个模型:

class SubCategory(models.Model):
    name = models.CharField('Nome della sottocategoria', max_length=30)

class Materials(models.Model):
    subcategory= models.ForeignKey(SubCategory)
    quantity=models.DecimalField()
    price=models.DecimalField()

class Costs_materials(models.Model):
    subcategory= models.ForeignKey(SubCategory)
    quantity=models.DecimalField()

在我看来,我设置了以下代码:

agg=Subcategory.objects.annotate(giacenza=Coalesce(Sum('materials__quantity'), 0)).annotate(
        utilizzato=Coalesce(Sum('costs_materials__quantity'), 0))

一切正常,但如果我尝试添加另一个注释功能,广告示例:

agg=Subcategory.objects.annotate(giacenza=Coalesce(Sum('materials__quantity'), 0)).annotate(
        utilizzato=Coalesce(Sum('costs_materials__quantity'), 0))

giacenza 和 utilizzato 的结果都不对,但结果却是对的两倍。如果结果必须为 10,则示例为 20

错误在哪里??

标签: pythonpython-3.xdjangodjango-modelsdjango-views

解决方案


推荐阅读