You can filter the number of fillings by annotating the request, then filtering it.
from django.db.models import Count
pizzas = Pizza.objects.annotate(
num_toppings=Count('toppings'),
).filter(num_toppings__lt=3)
Then you can use the prefetch_relatedsame as for other queries.
pizzas = Pizza.objects.annotate(
num_toppings=Count('toppings'),
).filter(num_toppings__lt=3).prefetch_related('toppings')
source
share