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?
You can use the annotation variable in the filter.
publishers=Publisher.objects.annotate(num_books=Count('book')).filter(num_books__gte=2)
2.1m questions
2.1m answers
60 comments
57.0k users