Car.objects.all() # 5 cars in db, every car costs 1000 $
Car.objects.all().aggregate(Sum("price")) # result: 5000
# aggregate only on a subset
Car.objects.all()[3:].aggregate(Sum("price")) # result: 5000!, not 3000
# with filter()[3:] i got the same results!
Why? Slicing is not evaluated in db?
How can i achieve that with aggregate?
It seems it is not possible to use aggregation on slicing as this open ticket suggest: https://code.djangoproject.com/ticket/12886
One solution is to execute two distinct queries. The first one to retrieve the sub set of Cars and the second one to actually perform the aggregation:
qs = Car.objects.all()[:3]
sub_sum = Car.objects.filter(pk__in=qs).aggregate(Sum("price"))
aggregate
works by modifying the query sent to the DB, causing aggregation to happen on the DB side. You have two choices.
filter
to reduce your QuerySet before using aggregate
, instead of slicing to reduce it. sum(car.price for car in cars)
after slicing. Once you slice, the query is sent to the database, so you can no longer aggregate through the ORM (i.e. QuerySet methods). If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With