mixins-single-object.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ====================
  2. Single object mixins
  3. ====================
  4. ``SingleObjectMixin``
  5. =====================
  6. .. class:: django.views.generic.detail.SingleObjectMixin
  7. Provides a mechanism for looking up an object associated with the
  8. current HTTP request.
  9. **Methods and Attributes**
  10. .. attribute:: model
  11. The model that this view will display data for. Specifying ``model
  12. = Foo`` is effectively the same as specifying ``queryset =
  13. Foo.objects.all()``, where ``objects`` stands for ``Foo``’s
  14. :ref:`default manager <default-managers>`.
  15. .. attribute:: queryset
  16. A ``QuerySet`` that represents the objects. If provided, the value of
  17. ``queryset`` supersedes the value provided for :attr:`model`.
  18. .. warning::
  19. ``queryset`` is a class attribute with a *mutable* value so care
  20. must be taken when using it directly. Before using it, either call
  21. its :meth:`~django.db.models.query.QuerySet.all` method or
  22. retrieve it with :meth:`get_queryset` which takes care of the
  23. cloning behind the scenes.
  24. .. attribute:: slug_field
  25. The name of the field on the model that contains the slug. By default,
  26. ``slug_field`` is ``'slug'``.
  27. .. attribute:: slug_url_kwarg
  28. The name of the URLConf keyword argument that contains the slug. By
  29. default, ``slug_url_kwarg`` is ``'slug'``.
  30. .. attribute:: pk_url_kwarg
  31. The name of the URLConf keyword argument that contains the primary key.
  32. By default, ``pk_url_kwarg`` is ``'pk'``.
  33. .. attribute:: context_object_name
  34. Designates the name of the variable to use in the context.
  35. .. attribute:: query_pk_and_slug
  36. If ``True``, causes :meth:`get_object()` to perform its lookup using
  37. both the primary key and the slug. Defaults to ``False``.
  38. This attribute can help mitigate `insecure direct object reference`_
  39. attacks. When applications allow access to individual objects by a
  40. sequential primary key, an attacker could brute-force guess all URLs;
  41. thereby obtaining a list of all objects in the application. If users
  42. with access to individual objects should be prevented from obtaining
  43. this list, setting ``query_pk_and_slug`` to ``True`` will help prevent
  44. the guessing of URLs as each URL will require two correct,
  45. non-sequential arguments. Using a unique slug may serve the same
  46. purpose, but this scheme allows you to have non-unique slugs.
  47. .. _insecure direct object reference: https://wiki.owasp.org/index.php/Top_10_2013-A4-Insecure_Direct_Object_References
  48. .. method:: get_object(queryset=None)
  49. Returns the single object that this view will display. If ``queryset``
  50. is provided, that queryset will be used as the source of objects;
  51. otherwise, :meth:`get_queryset` will be used. ``get_object()`` looks
  52. for a :attr:`pk_url_kwarg` argument in the arguments to the view; if
  53. this argument is found, this method performs a primary-key based lookup
  54. using that value. If this argument is not found, it looks for a
  55. :attr:`slug_url_kwarg` argument, and performs a slug lookup using the
  56. :attr:`slug_field`.
  57. When :attr:`query_pk_and_slug` is ``True``, ``get_object()`` will
  58. perform its lookup using both the primary key and the slug.
  59. .. method:: get_queryset()
  60. Returns the queryset that will be used to retrieve the object that
  61. this view will display. By default, :meth:`get_queryset` returns the
  62. value of the :attr:`queryset` attribute if it is set, otherwise
  63. it constructs a :class:`~django.db.models.query.QuerySet` by calling
  64. the ``all()`` method on the :attr:`model` attribute's default manager.
  65. .. method:: get_context_object_name(obj)
  66. Return the context variable name that will be used to contain the
  67. data that this view is manipulating. If :attr:`context_object_name` is
  68. not set, the context name will be constructed from the ``model_name``
  69. of the model that the queryset is composed from. For example, the model
  70. ``Article`` would have context object named ``'article'``.
  71. .. method:: get_context_data(**kwargs)
  72. Returns context data for displaying the object.
  73. The base implementation of this method requires that the ``self.object``
  74. attribute be set by the view (even if ``None``). Be sure to do this if
  75. you are using this mixin without one of the built-in views that does so.
  76. It returns a dictionary with these contents:
  77. * ``object``: The object that this view is displaying
  78. (``self.object``).
  79. * ``context_object_name``: ``self.object`` will also be stored under
  80. the name returned by :meth:`get_context_object_name`, which defaults
  81. to the lowercased version of the model name.
  82. .. admonition:: Context variables override values from template context processors
  83. Any variables from :meth:`get_context_data` take precedence over
  84. context variables from :ref:`context processors
  85. <subclassing-context-requestcontext>`. For example, if your view
  86. sets the :attr:`model` attribute to
  87. :class:`~django.contrib.auth.models.User`, the default context
  88. object name of ``user`` would override the ``user`` variable from
  89. the :func:`django.contrib.auth.context_processors.auth` context
  90. processor. Use :meth:`get_context_object_name` to avoid a clash.
  91. .. method:: get_slug_field()
  92. Returns the name of a slug field to be used to look up by slug. By
  93. default this returns the value of :attr:`slug_field`.
  94. ``SingleObjectTemplateResponseMixin``
  95. =====================================
  96. .. class:: django.views.generic.detail.SingleObjectTemplateResponseMixin
  97. A mixin class that performs template-based response rendering for views
  98. that operate upon a single object instance. Requires that the view it is
  99. mixed with provides ``self.object``, the object instance that the view is
  100. operating on. ``self.object`` will usually be, but is not required to be,
  101. an instance of a Django model. It may be ``None`` if the view is in the
  102. process of constructing a new instance.
  103. **Extends**
  104. * :class:`~django.views.generic.base.TemplateResponseMixin`
  105. **Methods and Attributes**
  106. .. attribute:: template_name_field
  107. The field on the current object instance that can be used to determine
  108. the name of a candidate template. If either ``template_name_field``
  109. itself or the value of the ``template_name_field`` on the current
  110. object instance is ``None``, the object will not be used for a
  111. candidate template name.
  112. .. attribute:: template_name_suffix
  113. The suffix to append to the auto-generated candidate template name.
  114. Default suffix is ``_detail``.
  115. .. method:: get_template_names()
  116. Returns a list of candidate template names. Returns the following list:
  117. * the value of ``template_name`` on the view (if provided)
  118. * the contents of the ``template_name_field`` field on the
  119. object instance that the view is operating upon (if available)
  120. * ``<app_label>/<model_name><template_name_suffix>.html``