On Wed, Apr 8, 2015 at 3:07 PM, Carsten Fuchs <carsten.fu...@cafu.de> wrote:

> 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.
>
> [...]
>
>   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 can help with this one. In fact you cant combine annotations with
filters. See

https://docs.djangoproject.com/en/1.8/topics/db/aggregation/#aggregations-and-other-queryset-clauses

"...When used with an annotate() clause, a filter has the effect of
constraining the objects for which an annotation is calculated. For
example..."

e.g.::


Store.objects.filter(books__pubdate__year=2014).annotate(min_2014_price=Min('books__price'),
max_2014_price=Max('books__price'))

HTH

-- 
Ramiro Morales
@ramiromorales

-- 
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/CAO7PdF8iqNxUpk%2B8-J3H%3D4JPDU6GUBZxdW_M6kOUb0K-8a3DRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to