Ticket #20659: ticket-20659.diff

File ticket-20659.diff, 2.4 KB (added by Baptiste Mispelon, 11 years ago)
  • docs/topics/class-based-views/mixins.txt

    diff --git a/docs/topics/class-based-views/mixins.txt b/docs/topics/class-based-views/mixins.txt
    index 84d7417..2c5df87 100644
    a b One way to do this is to combine :class:`ListView` with  
    286286for the paginated list of books can hang off the publisher found as the single
    287287object. In order to do this, we need to have two different querysets:
    288288
    289 ``Publisher`` queryset for use in
    290     :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
    291     We'll set the ``model`` attribute on the view and rely on the default
    292     implementation of ``get_object()`` to fetch the correct ``Publisher``
    293     object.
    294 
    295289``Book`` queryset for use by :class:`~django.views.generic.list.ListView`
    296     The default implementation of ``get_queryset()`` uses the ``model`` attribute
    297     to construct the queryset. This conflicts with our use of this attribute
    298     for ``get_object()`` so we'll override that method and have it return
    299     the queryset of ``Book`` objects linked to the ``Publisher`` we're looking
    300     at.
     290    Since we have access to the ``Publisher`` whose books we want to list, we
     291    simply override ``get_queryset()`` and use the ``Publisher``'s
     292    :ref:`reverse foreign key manager<backwards-related-objects>`.
     293
     294``Publisher`` queryset for use in :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
     295    We'll rely on the default implementation of ``get_object()`` to fetch the
     296    correct ``Publisher`` object.
     297    However, we need to explicitely pass a ``queryset`` argument because
     298    otherwise the default implementation of ``get_object()`` would call
     299    ``get_queryset()`` which we have overridden to return ``Book`` objects
     300    instead of ``Publisher`` ones.
    301301
    302302.. note::
    303303
    Now we can write a new ``PublisherDetail``::  
    317317    from books.models import Publisher
    318318
    319319    class PublisherDetail(SingleObjectMixin, ListView):
    320         model = Publisher  # for SingleObjectMixin.get_object
    321320        paginate_by = 2
    322321        template_name = "books/publisher_detail.html"
    323322
    324323        def get(self, request, *args, **kwargs):
    325             self.object = self.get_object()
     324            self.object = self.get_object(queryset=Publisher.objects.all())
    326325            return super(PublisherDetail, self).get(request, *args, **kwargs)
    327326
    328327        def get_context_data(self, **kwargs):
Back to Top