Django 按相关选项的数量筛选。德扬戈

Django 按相关选项的数量筛选。德扬戈,django,Django,我有以下看法: class IndexView(generic.ListView): template_name = "polls/index.html" context_object_name = "latest_poll_list" def get_queryset(self): """ Return the last five active published polls with at least two choi

我有以下看法:

class IndexView(generic.ListView):
    template_name = "polls/index.html"
    context_object_name = "latest_poll_list"

    def get_queryset(self):
        """
        Return the last five active published polls with at least
        two choices.
        """
        return Poll.objects.filter(pub_date__lte=timezone.now(),
                                   is_active__exact=True,
                                  ).order_by('-pub_date')[:5]
我只想公布有两个或更多选择的民意调查。 我尝试了许多变体,但都不能正常工作

我如何才能巧妙地实现这一点?

使用:

使用:

from django.db.models import Count

Poll.objects.filter(pub_date__lte=timezone.now(), is_active__exact=True) \
            .annotate(num_choices=Count('choice')) \
            .filter(num_choices__gte=2) \
            .order_by('-pub_date')[:5]