options.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. .. _ref-models-options:
  2. ======================
  3. Model ``Meta`` options
  4. ======================
  5. This document explains all the possible :ref:`metadata options <meta-options>` that you can give your model in its internal
  6. ``class Meta``.
  7. Available ``Meta`` options
  8. ==========================
  9. .. currentmodule:: django.db.models
  10. ``abstract``
  11. ------------
  12. .. attribute:: Options.abstract
  13. If ``True``, this model will be an :ref:`abstract base class <abstract-base-classes>`.
  14. ``db_table``
  15. ------------
  16. .. attribute:: Options.db_table
  17. The name of the database table to use for the model::
  18. db_table = 'music_album'
  19. .. _table-names:
  20. Table names
  21. ~~~~~~~~~~~
  22. To save you time, Django automatically derives the name of the database table
  23. from the name of your model class and the app that contains it. A model's
  24. database table name is constructed by joining the model's "app label" -- the
  25. name you used in ``manage.py startapp`` -- to the model's class name, with an
  26. underscore between them.
  27. For example, if you have an app ``bookstore`` (as created by
  28. ``manage.py startapp bookstore``), a model defined as ``class Book`` will have
  29. a database table named ``bookstore_book``.
  30. To override the database table name, use the ``db_table`` parameter in
  31. ``class Meta``.
  32. If your database table name is an SQL reserved word, or contains characters that
  33. aren't allowed in Python variable names -- notably, the hyphen -- that's OK.
  34. Django quotes column and table names behind the scenes.
  35. ``db_tablespace``
  36. -----------------
  37. .. attribute:: Options.db_tablespace
  38. .. versionadded:: 1.0
  39. The name of the database tablespace to use for the model. If the backend doesn't
  40. support tablespaces, this option is ignored.
  41. ``get_latest_by``
  42. -----------------
  43. .. attribute:: Options.get_latest_by
  44. The name of a :class:`DateField` or :class:`DateTimeField` in the model. This
  45. specifies the default field to use in your model :class:`Manager`'s
  46. :class:`~QuerySet.latest` method.
  47. Example::
  48. get_latest_by = "order_date"
  49. See the docs for :meth:`~django.db.models.QuerySet.latest` for more.
  50. ``managed``
  51. -----------------------
  52. .. attribute:: Options.managed
  53. .. versionadded:: 1.1
  54. Defaults to ``True``, meaning Django will create the appropriate database
  55. tables in :ref:`django-admin-syncdb` and remove them as part of a :ref:`reset
  56. <django-admin-reset>` management command. That is, Django *manages* the
  57. database tables' lifecycles.
  58. If ``False``, no database table creation or deletion operations will be
  59. performed for this model. This is useful if the model represents an existing
  60. table or a database view that has been created by some other means. This is
  61. the *only* difference when ``managed`` is ``False``. All other aspects of
  62. model handling are exactly the same as normal. This includes
  63. 1. Adding an automatic primary key field to the model if you don't declare
  64. it. To avoid confusion for later code readers, it's recommended to
  65. specify all the columns from the database table you are modeling when
  66. using unmanaged models.
  67. 2. If a model contains a :class:`~django.db.models.ManyToManyField` and
  68. has ``managed=False``, the intermediate table for the many-to-many join
  69. will also not be created. Should you require the intermediate table to
  70. be created, set it up as an explicit model and use the
  71. :attr:`ManyToManyField.through` attribute.
  72. For tests involving models with ``managed=False``, it's up to you to ensure
  73. the correct tables are created as part of the test setup.
  74. If you're interested in changing the Python-level behavior of a model class,
  75. you *could* use ``managed=False`` and create a copy of an existing model.
  76. However, there's a better approach for that situation: :ref:`proxy-models`.
  77. ``order_with_respect_to``
  78. -------------------------
  79. .. attribute:: Options.order_with_respect_to
  80. Marks this object as "orderable" with respect to the given field. This is almost
  81. always used with related objects to allow them to be ordered with respect to a
  82. parent object. For example, if an ``Answer`` relates to a ``Question`` object,
  83. and a question has more than one answer, and the order of answers matters, you'd
  84. do this::
  85. class Answer(models.Model):
  86. question = models.ForeignKey(Question)
  87. # ...
  88. class Meta:
  89. order_with_respect_to = 'question'
  90. ``ordering``
  91. ------------
  92. .. attribute:: Options.ordering
  93. The default ordering for the object, for use when obtaining lists of objects::
  94. ordering = ['-order_date']
  95. This is a tuple or list of strings. Each string is a field name with an optional
  96. "-" prefix, which indicates descending order. Fields without a leading "-" will
  97. be ordered ascending. Use the string "?" to order randomly.
  98. .. note::
  99. Regardless of how many fields are in :attr:`~Options.ordering`, the admin
  100. site uses only the first field.
  101. For example, to order by a ``pub_date`` field ascending, use this::
  102. ordering = ['pub_date']
  103. To order by ``pub_date`` descending, use this::
  104. ordering = ['-pub_date']
  105. To order by ``pub_date`` descending, then by ``author`` ascending, use this::
  106. ordering = ['-pub_date', 'author']
  107. ``permissions``
  108. ---------------
  109. .. attribute:: Options.permissions
  110. Extra permissions to enter into the permissions table when creating this object.
  111. Add, delete and change permissions are automatically created for each object
  112. that has ``admin`` set. This example specifies an extra permission,
  113. ``can_deliver_pizzas``::
  114. permissions = (("can_deliver_pizzas", "Can deliver pizzas"),)
  115. This is a list or tuple of 2-tuples in the format ``(permission_code,
  116. human_readable_permission_name)``.
  117. ``proxy``
  118. ---------
  119. .. attribute:: Options.proxy
  120. .. versionadded:: 1.1
  121. If set to ``True``, a model which subclasses another model will be treated as
  122. a :ref:`proxy model <proxy-models>`.
  123. ``unique_together``
  124. -------------------
  125. .. attribute:: Options.unique_together
  126. Sets of field names that, taken together, must be unique::
  127. unique_together = (("driver", "restaurant"),)
  128. This is a list of lists of fields that must be unique when considered together.
  129. It's used in the Django admin and is enforced at the database level (i.e., the
  130. appropriate ``UNIQUE`` statements are included in the ``CREATE TABLE``
  131. statement).
  132. .. versionadded:: 1.0
  133. For convenience, unique_together can be a single list when dealing with a single
  134. set of fields::
  135. unique_together = ("driver", "restaurant")
  136. ``verbose_name``
  137. ----------------
  138. .. attribute:: Options.verbose_name
  139. A human-readable name for the object, singular::
  140. verbose_name = "pizza"
  141. If this isn't given, Django will use a munged version of the class name:
  142. ``CamelCase`` becomes ``camel case``.
  143. ``verbose_name_plural``
  144. -----------------------
  145. .. attribute:: Options.verbose_name_plural
  146. The plural name for the object::
  147. verbose_name_plural = "stories"
  148. If this isn't given, Django will use :attr:`~Options.verbose_name` + ``"s"``.