1👍
You can use annotate
for this sort of thing – that will add an extra aggregate column and filter on that:
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects \
.annotate(choices=Count('choice')) \
.filter(pub_date__lte=timezone.now(), choices__gt=0) \
.order_by('-pub_date')[:5]
Now the resulting Question objects will also have a choices
attribute set to the number of choices, which you can use later on.
👤fips
Source:stackexchange.com