I have three models:
class Variety(models.Model)
name = models.CharField(max_length=24)
class Item(models.Model)
name = models.CharField(max_length=24)
in_stock = models.IntegerField()
class ItemPart(models.Model)
variety = models.ForeignKey(Variety)
product = models.ForeignKey(Product)
qty = models.IntegerField()
I would like to tell how much of each Variety has been made into Items by getting all the related ItemParts and multiplying their qty
by the in_stock
of their related Items.
I've gotten this far:
Variety.objects.all().prefetch_related('itempart_set').values('name').annotate(
Sum(F("itempart_set__qty") * F("itempart_set__item_set__in_stock")
)
Will this work? Will it sum the products, or will it simply multiply the sums?