mixins-single-object.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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()``.
  14. .. attribute:: queryset
  15. A ``QuerySet`` that represents the objects. If provided, the value of
  16. ``queryset`` supersedes the value provided for :attr:`model`.
  17. .. warning::
  18. ``queryset`` is a class attribute with a *mutable* value so care
  19. must be taken when using it directly. Before using it, either call
  20. its :meth:`~django.db.models.query.QuerySet.all` method or
  21. retrieve it with :meth:`get_queryset` which takes care of the
  22. cloning behind the scenes.
  23. .. attribute:: slug_field
  24. The name of the field on the model that contains the slug. By default,
  25. ``slug_field`` is ``'slug'``.
  26. .. attribute:: slug_url_kwarg
  27. The name of the URLConf keyword argument that contains the slug. By
  28. default, ``slug_url_kwarg`` is ``'slug'``.
  29. .. attribute:: pk_url_kwarg
  30. The name of the URLConf keyword argument that contains the primary key.
  31. By default, ``pk_url_kwarg`` is ``'pk'``.
  32. .. attribute:: context_object_name
  33. Designates the name of the variable to use in the context.
  34. .. method:: get_object(queryset=None)
  35. Returns the single object that this view will display. If
  36. ``queryset`` is provided, that queryset will be used as the
  37. source of objects; otherwise, :meth:`get_queryset` will be used.
  38. ``get_object()`` looks for a :attr:`pk_url_kwarg` argument in the
  39. arguments to the view; if this argument is found, this method performs
  40. a primary-key based lookup using that value. If this argument is not
  41. found, it looks for a :attr:`slug_url_kwarg` argument, and performs a
  42. slug lookup using the :attr:`slug_field`.
  43. .. method:: get_queryset()
  44. Returns the queryset that will be used to retrieve the object that
  45. this view will display. By default, :meth:`get_queryset` returns the
  46. value of the :attr:`queryset` attribute if it is set, otherwise
  47. it constructs a :class:`~django.db.models.query.QuerySet` by calling
  48. the ``all()`` method on the :attr:`model` attribute's default manager.
  49. .. method:: get_context_object_name(obj)
  50. Return the context variable name that will be used to contain the
  51. data that this view is manipulating. If :attr:`context_object_name` is
  52. not set, the context name will be constructed from the ``object_name``
  53. of the model that the queryset is composed from. For example, the model
  54. ``Article`` would have context object named ``'article'``.
  55. .. method:: get_context_data(**kwargs)
  56. Returns context data for displaying the list of objects.
  57. .. method:: get_slug_field()
  58. Returns the name of a slug field to be used to look up by slug. By
  59. default this simply returns the value of :attr:`slug_field`.
  60. **Context**
  61. * ``object``: The object that this view is displaying. If
  62. ``context_object_name`` is specified, that variable will also be
  63. set in the context, with the same value as ``object``.
  64. SingleObjectTemplateResponseMixin
  65. ---------------------------------
  66. .. class:: django.views.generic.detail.SingleObjectTemplateResponseMixin
  67. A mixin class that performs template-based response rendering for views
  68. that operate upon a single object instance. Requires that the view it is
  69. mixed with provides ``self.object``, the object instance that the view is
  70. operating on. ``self.object`` will usually be, but is not required to be,
  71. an instance of a Django model. It may be ``None`` if the view is in the
  72. process of constructing a new instance.
  73. **Extends**
  74. * :class:`~django.views.generic.base.TemplateResponseMixin`
  75. **Methods and Attributes**
  76. .. attribute:: template_name_field
  77. The field on the current object instance that can be used to determine
  78. the name of a candidate template. If either ``template_name_field``
  79. itself or the value of the ``template_name_field`` on the current
  80. object instance is ``None``, the object will not be used for a
  81. candidate template name.
  82. .. attribute:: template_name_suffix
  83. The suffix to append to the auto-generated candidate template name.
  84. Default suffix is ``_detail``.
  85. .. method:: get_template_names()
  86. Returns a list of candidate template names. Returns the following list:
  87. * the value of ``template_name`` on the view (if provided)
  88. * the contents of the ``template_name_field`` field on the
  89. object instance that the view is operating upon (if available)
  90. * ``<app_label>/<object_name><template_name_suffix>.html``