Dear Django fellows,

at https://docs.djangoproject.com/en/1.8/topics/db/aggregation/#joins-and-aggregates the first example is:

>>> from django.db.models import Max, Min
>>> Store.objects.annotate(min_price=Min('books__price'), 
max_price=Max('books__price'))

which will annotate each Store object in the QuerySet with the minimum and maximum prices that its books have.

What I was wondering is:


1) Is there a way to annotate each Store object with the actual Book objects related to the minimum and maximum prices?

That is, if `s` is a Store object from the above QuerySet and we can access

    s.min_price
    s.max_price

would it also be possible to have

    s.min_book   # The Book object whose price is minimal
    s.max_book   # The Book object whose price is maximal

?


2) Can this annotation be filtered? For example, if for each Store we wanted to learn the min and max prices of books published in 2014, can this be done?


I'd be very grateful for any hints!  :-)

Best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/55256E73.8020203%40cafu.de.
For more options, visit https://groups.google.com/d/optout.

Reply via email to