On Feb 21, 6:11 am, Enrico <[email protected]> wrote: > This query: > Publisher.objects.filter(book__rating__gt=3.0).annotate(num_books=Count('bo > ok')) > returns all the publishers with at least one good book (ranked > 3) > with annotated the number of good books for each publisher. > > How can i modify the query to get the list of ALL publishers with > annotated the number of good books for each publisher? > In other words I want to keep in the results also the Publishers > without good books (num_books = 0).
Hi Enrico, The Django documentation has an example of what you are trying to do: https://docs.djangoproject.com/en/1.3/topics/db/aggregation/#generating-aggregates-for-each-item-in-a-queryset The answer to your specific question is to do it this way: qs = Publisher.objects.annotate(num_books=Count('book')) for o in qs: print o.num_books -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

