Now let's take a closer look at the model
argument we've been using all along. The model
argument, which specifies the database model that the view will operate upon, is available on all the generic views that operate on a single object or a collection of objects. However, the model
argument is not the only way to specify the objects that the view will operate upon-you can also specify the list of objects using the queryset
argument:
from django.views.generic import DetailView from books.models import Publisher class PublisherDetail(DetailView): context_object_name = 'publisher' queryset = Publisher.objects.all()
Specifying model = Publisher
is really just shorthand for saying queryset = Publisher.objects.all()
. However, by using queryset
to define a filtered list of objects you can be more specific about the objects that will be visible in the view. To pick a simple example, we might want to order a list of books...