mixins-single-object.txt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. .. versionadded:: 1.8
  37. If ``True``, causes :meth:`get_object()` to perform its lookup using
  38. both the primary key and the slug. Defaults to ``False``.
  39. This attribute can help mitigate `insecure direct object reference`_
  40. attacks. When applications allow access to individual objects by a
  41. sequential primary key, an attacker could brute-force guess all URLs;
  42. thereby obtaining a list of all objects in the application. If users
  43. with access to individual objects should be prevented from obtaining
  44. this list, setting ``query_pk_and_slug`` to ``True`` will help prevent
  45. the guessing of URLs as each URL will require two correct,
  46. non-sequential arguments. Simply using a unique slug may serve the same
  47. purpose, but this scheme allows you to have non-unique slugs.
  48. .. _insecure direct object reference: https://www.owasp.org/index.php/Top_10_2013-A4-Insecure_Direct_Object_References
  49. .. method:: get_object(queryset=None)
  50. Returns the single object that this view will display. If ``queryset``
  51. is provided, that queryset will be used as the source of objects;
  52. otherwise, :meth:`get_queryset` will be used. ``get_object()`` looks
  53. for a :attr:`pk_url_kwarg` argument in the arguments to the view; if
  54. this argument is found, this method performs a primary-key based lookup
  55. using that value. If this argument is not found, it looks for a
  56. :attr:`slug_url_kwarg` argument, and performs a slug lookup using the
  57. :attr:`slug_field`.
  58. .. versionchanged:: 1.8
  59. When :attr:`query_pk_and_slug` is ``True``, ``get_object()`` will
  60. perform its lookup using both the primary key and the slug.
  61. .. method:: get_queryset()
  62. Returns the queryset that will be used to retrieve the object that
  63. this view will display. By default, :meth:`get_queryset` returns the
  64. value of the :attr:`queryset` attribute if it is set, otherwise
  65. it constructs a :class:`~django.db.models.query.QuerySet` by calling
  66. the ``all()`` method on the :attr:`model` attribute's default manager.
  67. .. method:: get_context_object_name(obj)
  68. Return the context variable name that will be used to contain the
  69. data that this view is manipulating. If :attr:`context_object_name` is
  70. not set, the context name will be constructed from the ``model_name``
  71. of the model that the queryset is composed from. For example, the model
  72. ``Article`` would have context object named ``'article'``.
  73. .. method:: get_context_data(**kwargs)
  74. Returns context data for displaying the list of objects.
  75. The base implementation of this method requires that the ``object``
  76. attribute be set by the view (even if ``None``). Be sure to do this if
  77. you are using this mixin without one of the built-in views that does so.
  78. .. method:: get_slug_field()
  79. Returns the name of a slug field to be used to look up by slug. By
  80. default this simply returns the value of :attr:`slug_field`.
  81. **Context**
  82. * ``object``: The object that this view is displaying. If
  83. ``context_object_name`` is specified, that variable will also be
  84. set in the context, with the same value as ``object``.
  85. SingleObjectTemplateResponseMixin
  86. ---------------------------------
  87. .. class:: django.views.generic.detail.SingleObjectTemplateResponseMixin
  88. A mixin class that performs template-based response rendering for views
  89. that operate upon a single object instance. Requires that the view it is
  90. mixed with provides ``self.object``, the object instance that the view is
  91. operating on. ``self.object`` will usually be, but is not required to be,
  92. an instance of a Django model. It may be ``None`` if the view is in the
  93. process of constructing a new instance.
  94. **Extends**
  95. * :class:`~django.views.generic.base.TemplateResponseMixin`
  96. **Methods and Attributes**
  97. .. attribute:: template_name_field
  98. The field on the current object instance that can be used to determine
  99. the name of a candidate template. If either ``template_name_field``
  100. itself or the value of the ``template_name_field`` on the current
  101. object instance is ``None``, the object will not be used for a
  102. candidate template name.
  103. .. attribute:: template_name_suffix
  104. The suffix to append to the auto-generated candidate template name.
  105. Default suffix is ``_detail``.
  106. .. method:: get_template_names()
  107. Returns a list of candidate template names. Returns the following list:
  108. * the value of ``template_name`` on the view (if provided)
  109. * the contents of the ``template_name_field`` field on the
  110. object instance that the view is operating upon (if available)
  111. * ``<app_label>/<model_name><template_name_suffix>.html``