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
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:
sum([box.quantity_available * box.items_in_box for box in Box.objects.all()]
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.