【发布时间】:2013-11-11 12:01:57
【问题描述】:
这两段代码乍一看是相同的:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'
queryset = Poll.active.order_by('-pub_date')[:5]
和
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'
def get_queryset(self):
return Poll.active.order_by('-pub_date')[:5]
它们之间有什么区别吗?如果是:
什么方法更好?或者当设置queryset 变量优于覆盖get_queryset 方法时?反之亦然。
【问题讨论】:
标签: django django-views django-generic-views