2
0

mixins-multiple-object.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ======================
  2. Multiple object mixins
  3. ======================
  4. ``MultipleObjectMixin``
  5. =======================
  6. .. class:: django.views.generic.list.MultipleObjectMixin
  7. A mixin that can be used to display a list of objects.
  8. If ``paginate_by`` is specified, Django will paginate the results returned
  9. by this. You can specify the page number in the URL in one of two ways:
  10. * Use the ``page`` parameter in the URLconf. For example, this is what
  11. your URLconf might look like::
  12. path("objects/page<int:page>/", PaginatedView.as_view()),
  13. * Pass the page number via the ``page`` query-string parameter. For
  14. example, a URL would look like this:
  15. .. code-block:: text
  16. /objects/?page=3
  17. These values and lists are 1-based, not 0-based, so the first page would be
  18. represented as page ``1``.
  19. For more on pagination, read the :doc:`pagination documentation
  20. </topics/pagination>`.
  21. As a special case, you are also permitted to use ``last`` as a value for
  22. ``page``:
  23. .. code-block:: text
  24. /objects/?page=last
  25. This allows you to access the final page of results without first having to
  26. determine how many pages there are.
  27. Note that ``page`` *must* be either a valid page number or the value
  28. ``last``; any other value for ``page`` will result in a 404 error.
  29. **Extends**
  30. * :class:`django.views.generic.base.ContextMixin`
  31. **Methods and Attributes**
  32. .. attribute:: allow_empty
  33. A boolean specifying whether to display the page if no objects are
  34. available. If this is ``False`` and no objects are available, the view
  35. will raise a 404 instead of displaying an empty page. By default, this
  36. is ``True``.
  37. .. attribute:: model
  38. The model that this view will display data for. Specifying ``model
  39. = Foo`` is effectively the same as specifying ``queryset =
  40. Foo.objects.all()``, where ``objects`` stands for ``Foo``’s
  41. :ref:`default manager <default-managers>`.
  42. .. attribute:: queryset
  43. A ``QuerySet`` that represents the objects. If provided, the value of
  44. ``queryset`` supersedes the value provided for :attr:`model`.
  45. .. warning::
  46. ``queryset`` is a class attribute with a *mutable* value so care
  47. must be taken when using it directly. Before using it, either call
  48. its :meth:`~django.db.models.query.QuerySet.all` method or
  49. retrieve it with :meth:`get_queryset` which takes care of the
  50. cloning behind the scenes.
  51. .. attribute:: ordering
  52. A string or list of strings specifying the ordering to apply to the ``queryset``.
  53. Valid values are the same as those for :meth:`~django.db.models.query.QuerySet.order_by`.
  54. .. attribute:: paginate_by
  55. An integer specifying how many objects should be displayed per page. If
  56. this is given, the view will paginate objects with
  57. ``paginate_by`` objects per page. The view will
  58. expect either a ``page`` query string parameter (via ``request.GET``)
  59. or a ``page`` variable specified in the URLconf.
  60. .. attribute:: paginate_orphans
  61. An integer specifying the number of "overflow" objects the last page
  62. can contain. This extends the :attr:`paginate_by` limit on the last
  63. page by up to ``paginate_orphans``, in order to keep the last page from
  64. having a very small number of objects.
  65. .. attribute:: page_kwarg
  66. A string specifying the name to use for the page parameter.
  67. The view will expect this parameter to be available either as a query
  68. string parameter (via ``request.GET``) or as a kwarg variable specified
  69. in the URLconf. Defaults to ``page``.
  70. .. attribute:: paginator_class
  71. The paginator class to be used for pagination. By default,
  72. :class:`django.core.paginator.Paginator` is used. If the custom paginator
  73. class doesn't have the same constructor interface as
  74. :class:`django.core.paginator.Paginator`, you will also need to
  75. provide an implementation for :meth:`get_paginator`.
  76. .. attribute:: context_object_name
  77. Designates the name of the variable to use in the context.
  78. .. method:: get_queryset()
  79. Get the list of items for this view. This must be an iterable and may
  80. be a queryset (in which queryset-specific behavior will be enabled).
  81. .. method:: get_ordering()
  82. Returns a string (or iterable of strings) that defines the ordering that
  83. will be applied to the ``queryset``.
  84. Returns :attr:`ordering` by default.
  85. .. method:: paginate_queryset(queryset, page_size)
  86. Returns a 4-tuple containing (``paginator``, ``page``, ``object_list``,
  87. ``is_paginated``).
  88. Constructed by paginating ``queryset`` into pages of size ``page_size``.
  89. If the request contains a ``page`` argument, either as a captured URL
  90. argument or as a GET argument, ``object_list`` will correspond to the
  91. objects from that page.
  92. .. method:: get_paginate_by(queryset)
  93. Returns the number of items to paginate by, or ``None`` for no
  94. pagination. By default this returns the value of :attr:`paginate_by`.
  95. .. method:: get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)
  96. Returns an instance of the paginator to use for this view. By default,
  97. instantiates an instance of :attr:`paginator_class`.
  98. .. method:: get_paginate_orphans()
  99. An integer specifying the number of "overflow" objects the last page
  100. can contain. By default this returns the value of
  101. :attr:`paginate_orphans`.
  102. .. method:: get_allow_empty()
  103. Return a boolean specifying whether to display the page if no objects
  104. are available. If this method returns ``False`` and no objects are
  105. available, the view will raise a 404 instead of displaying an empty
  106. page. By default, this is ``True``.
  107. .. method:: get_context_object_name(object_list)
  108. Return the context variable name that will be used to contain
  109. the list of data that this view is manipulating. If
  110. ``object_list`` is a queryset of Django objects and
  111. :attr:`context_object_name` is not set,
  112. the context name will be the ``model_name`` of the model that
  113. the queryset is composed from, with postfix ``'_list'``
  114. appended. For example, the model ``Article`` would have a
  115. context object named ``article_list``.
  116. .. method:: get_context_data(**kwargs)
  117. Returns context data for displaying the list of objects.
  118. **Context**
  119. * ``object_list``: The list of objects that this view is displaying. If
  120. ``context_object_name`` is specified, that variable will also be set
  121. in the context, with the same value as ``object_list``.
  122. * ``is_paginated``: A boolean representing whether the results are
  123. paginated. Specifically, this is set to ``False`` if no page size has
  124. been specified, or if the available objects do not span multiple
  125. pages.
  126. * ``paginator``: An instance of
  127. :class:`django.core.paginator.Paginator`. If the page is not
  128. paginated, this context variable will be ``None``.
  129. * ``page_obj``: An instance of
  130. :class:`django.core.paginator.Page`. If the page is not paginated,
  131. this context variable will be ``None``.
  132. ``MultipleObjectTemplateResponseMixin``
  133. =======================================
  134. .. class:: django.views.generic.list.MultipleObjectTemplateResponseMixin
  135. A mixin class that performs template-based response rendering for views
  136. that operate upon a list of object instances. Requires that the view it is
  137. mixed with provides ``self.object_list``, the list of object instances that
  138. the view is operating on. ``self.object_list`` may be, but is not required
  139. to be, a :class:`~django.db.models.query.QuerySet`.
  140. **Extends**
  141. * :class:`~django.views.generic.base.TemplateResponseMixin`
  142. **Methods and Attributes**
  143. .. attribute:: template_name_suffix
  144. The suffix to append to the auto-generated candidate template name.
  145. Default suffix is ``_list``.
  146. .. method:: get_template_names()
  147. Returns a list of candidate template names. Returns the following list:
  148. * the value of ``template_name`` on the view (if provided)
  149. * ``<app_label>/<model_name><template_name_suffix>.html``