mixins-single-object.txt 4.7 KB

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