class Inventory(models.Model):
...
product = models.ForeignKey('Product')
quantity = models.IntegerField(default=0)
class Order(models.Model):
...
quantity = models.IntegerField()
inventory = models.ForeignKey('Inventory')
active = models.BooleanField(default=True)
# Not considering the orders that are not active
queryset = Inventory.objects.annotate(
used=Sum('order__quantity')
).filter(product=product)
I need to get queryset of inventory that has annotated 'used' value. 'used' value is determined by quantity of all related orders but are active.
Edit: To be more precise, I need to SUM quantity of only active orders.