Consider these pseudo models:
class BaseProduct:
quantity_available = Integer
class Box(BaseProduct):
items_in_box = Integer
>> BaseProduct.objects.count()
>> Integer
But how do I retrieve the total number of products, so:
for each object[quantity_available * items_in_box] * total_objects
Solution:
I used Simeon Visser’s ‘sum’ as a partial solution, adding a property to the Base class:
@property
def _box_count(self):
try:
return self.items_in_box * self.quantity_available
except AttributeError:
return self.quantity_available
sum([item._box_count for item in BaseProduct.objects.all()])
Try the following:
It retrieves all the boxes and for each box, it computes the quanitity available of that box times the number of items in boxes of that type. Lastly, it sums all these values so you get the total number of products.