tags:

views:

31

answers:

1

Taking the example from: http://docs.djangoproject.com/en/dev/topics/db/aggregation/#filter-and-exclude

Publisher.objects.filter(book__rating__gt=3.0).annotate(num_books=Count('book'))

Is there anyway to have the filter only apply to the annotation, so it would return all publishers, with some having a num_books=0?

A: 

You can use the annotation variable in the filter.

publishers=Publisher.objects.annotate(num_books=Count('book')).filter(num_books__gte=2)
czarchaic
No, this is not what I want.
Collin Anderson
Uhhh...what do you want...
czarchaic
I mean, I want all publishers to be returned, and each publisher should be annotated with the number of books with high ratings. So, included in the publisher list should be publishers with less than 2 books with high ratings, and publishers with only low rated books, and publishers who have no books at all. Let me know if I can clarify that better.
Collin Anderson