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