Django 通用 ListView:上下文中的重复查询集

Django generic ListView: duplicate queryset in the context

我正在使用 Django (1.9.1) 的通用 ListView。我自定义了要放在上下文中的查询集的名称(我称之为 content_list)。但令人惊讶的是,当我查看上下文内容时,我可以看到 object_list 以及 content_list。如果列表很大,这不是很优化。我怎样才能摆脱object_list?。这是我的观点:

class Home(ListView): #TemplateView
    context_object_name = 'content_list'
    template_name = 'website/index.html'
    paginate_by = CONTENT_PAGINATE_BY

    def get_queryset(self):
        cc_id = self.kwargs.get('cc_id')
        if cc_id != None:
            qs = Content.objects.filter(category=cc_id)
        else:
            qs = Content.objects.all()
        return qs.order_by('-created_on')

    def get_context_data(self, **kwargs):
        context = super(Home, self).get_context_data(**kwargs)
        context['content_category_list'] = ContentCategory.objects.all()
        print(context)
        return context

我很确定它们都引用了内存中的同一个列表。

来自docs

Well, if you’re dealing with a model object, this is already done for you. When you are dealing with an object or queryset, Django is able to populate the context using the lower cased version of the model class’ name. This is provided in addition to the default object_list entry, but contains exactly the same data, i.e. publisher_list.

除此之外,即使它们没有引用相同的数据,您也会忘记查询集是延迟执行的,因此如果您从不使用另一个列表,那么它就永远不会执行。

这是设计使然。这不是与数据库的另一个交互,而是第二个引用。