简体   繁体   中英

django get_queryset() is redundant, since we have get_context_data()?

I have been always wondering if get_queryset() is redundant in django. As we know, get_queryset() returns a list of objects back to the page, but to return this list of objects, we can also always specify it in get_context_data(), and get_context_data() can return many more variables, not just a list. An example can be seen as follows:

Return a list of children book by get_queryset()

from django.views.generic import ListView
from books.models import Book

class ChildrenBook(ListView):
    model = Book
    def get_queryset(self, *args, **kwargs):
        qset = super(BookListView, self).get_queryset(*args, **kwargs)
        return qset.filter(type="children")

Return a list of children book by get_context_data()

from django.views.generic import ListView
from books.models import Book

class ChildrenBook(ListView):
    model = Book
    def get_context_data(self, **kwargs):
        context = super(PublisherDetail, self).get_context_data(**kwargs)
        context['children_book_list'] = Book.objects.filter(type="children")
        return context

To the best of my knowledge, I do not find anything that get_queryset() can do but get_context_data cannot. Anyone can find some situation where only get_queryset() can be used and illustrate its necessity?

If you take a look at the actual django source code you'll see why overloading only get_context_data is a bad idea unless you want to have to rewrite a whole bunch of code:

class BaseListView(MultipleObjectMixin, View):
    """
    A base view for displaying a list of objects.
    """
    def get(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        allow_empty = self.get_allow_empty()

        if not allow_empty:
            # When pagination is enabled and object_list is a queryset,
            # it's better to do a cheap query than to load the unpaginated
            # queryset in memory.
            if (self.get_paginate_by(self.object_list) is not None
                    and hasattr(self.object_list, 'exists')):
                is_empty = not self.object_list.exists()
            else:
                is_empty = len(self.object_list) == 0
            if is_empty:
                raise Http404(_("Empty list and '%(class_name)s.allow_empty' is False.")
                        % {'class_name': self.__class__.__name__})
        context = self.get_context_data()
        return self.render_to_response(context)

Essentially, not overloading the get_queryset method won't handle cases like when there are no records in the filter because the object_list property won't be set correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM