Taking the example from: http://docs.djangoproject.com/en/dev/topics/db/aggregation/#filter-and-exclude
Publisher.objects.filter(book__rating__gt=3.0).annota
Starting Django 2.0 one may use filter parameter of aggregation function:
from django.db.models import Q Publisher.objects.annotate(num_books=Count('book', filter=Q(book__rating__gt=3.0)))
The answer is based on a cheat sheet.