These two pieces of code are identical at the first blush:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'
queryset = Poll.active.order_by('-pub_date')[:5]
and
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]
Is there any difference between them? And if it is:
What approach is better? Or when setting queryset
variable is better than override the get_queryset
method? And vice versa.
get_queryset
self.model.objects.filter(...)
. In case of inheriting own listviews it is worth to remember that one should refer tosuper(YourListViewExtendingSomeOtherLV, self).get_queryset().filter(...)
– Craquelure