On Mar 3, 8:55 am, Ross <real...@gmail.com> wrote:
> I have started using aggregation, but it seems to ignore any slicing I
> do on a QuerySet before calling aggregate. This is what I'm doing:
>
> Product.objects.order_by("-price")[:100].values("price").aggregate(Max
> ("price"), Min("price"))
>
> I want the maximum and minimum price for the products with the top 100
> largest prices. The aggregate function, however, returns the maximum
> and minimum price for all Products--it ignores the [:100] slice.
>

You do realize that because your ordering by "price" your min and max
will be the first and last items in the returned QuerySet, don't you?
So something like:

>>> qs = Product.objects.order_by("-price")[:100]
>>> max = qs.values("price")[0]
>>> min = qs.values("price")[-1]

The above is untested, but you should get the idea. Yeah, it would
seem that aggregation would be nice for this, but if this works...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to