I have set the following three models in my django app:
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()
In my views I have set the following code:
agg=Subcategory.objects.annotate(giacenza=Coalesce(Sum('materials__quantity'), 0)).annotate(
utilizzato=Coalesce(Sum('costs_materials__quantity'), 0))
All works perfectly but If I try to add another annotate function, ad example:
agg=Subcategory.objects.annotate(giacenza=Coalesce(Sum('materials__quantity'), 0)).annotate(
utilizzato=Coalesce(Sum('costs_materials__quantity'), 0))
The result of giacenza and utilizzato are not right, but the result is double than the right. Ad example if the result must be 10, is 20.
Where is the error??