options.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. If ``False``, no database table creation or deletion operations will be
  55. performed for this model. This is useful if the model represents an existing
  56. table or a database view that has been created by some other means.
  57. The default value is ``True``, meaning Django will create the appropriate
  58. database tables in :ref:`django-admin-syncdb` and remove them as part of a
  59. :ref:`reset <django-admin-reset>` management command.
  60. If a model contains a :class:`~django.db.models.ManyToManyField` and has
  61. ``managed=False``, the intermediate table for the many-to-many join will also
  62. not be created. Should you require the intermediate table to be created, set
  63. it up as an explicit model and use the :attr:`ManyToManyField.through`
  64. attribute.
  65. For tests involving models with ``managed=False``, it's up to you to ensure
  66. the correct tables are created as part of the test setup.
  67. ``order_with_respect_to``
  68. -------------------------
  69. .. attribute:: Options.order_with_respect_to
  70. Marks this object as "orderable" with respect to the given field. This is almost
  71. always used with related objects to allow them to be ordered with respect to a
  72. parent object. For example, if an ``Answer`` relates to a ``Question`` object,
  73. and a question has more than one answer, and the order of answers matters, you'd
  74. do this::
  75. class Answer(models.Model):
  76. question = models.ForeignKey(Question)
  77. # ...
  78. class Meta:
  79. order_with_respect_to = 'question'
  80. ``ordering``
  81. ------------
  82. .. attribute:: Options.ordering
  83. The default ordering for the object, for use when obtaining lists of objects::
  84. ordering = ['-order_date']
  85. This is a tuple or list of strings. Each string is a field name with an optional
  86. "-" prefix, which indicates descending order. Fields without a leading "-" will
  87. be ordered ascending. Use the string "?" to order randomly.
  88. .. note::
  89. Regardless of how many fields are in :attr:`~Options.ordering`, the admin
  90. site uses only the first field.
  91. For example, to order by a ``pub_date`` field ascending, use this::
  92. ordering = ['pub_date']
  93. To order by ``pub_date`` descending, use this::
  94. ordering = ['-pub_date']
  95. To order by ``pub_date`` descending, then by ``author`` ascending, use this::
  96. ordering = ['-pub_date', 'author']
  97. ``permissions``
  98. ---------------
  99. .. attribute:: Options.permissions
  100. Extra permissions to enter into the permissions table when creating this object.
  101. Add, delete and change permissions are automatically created for each object
  102. that has ``admin`` set. This example specifies an extra permission,
  103. ``can_deliver_pizzas``::
  104. permissions = (("can_deliver_pizzas", "Can deliver pizzas"),)
  105. This is a list or tuple of 2-tuples in the format ``(permission_code,
  106. human_readable_permission_name)``.
  107. ``proxy``
  108. ---------
  109. .. attribute:: Options.proxy
  110. .. versionadded: 1.1
  111. If set to ``True``, a model which subclasses another model will be treated as
  112. a :ref:`proxy model <proxy-models>`.
  113. ``unique_together``
  114. -------------------
  115. .. attribute:: Options.unique_together
  116. Sets of field names that, taken together, must be unique::
  117. unique_together = (("driver", "restaurant"),)
  118. This is a list of lists of fields that must be unique when considered together.
  119. It's used in the Django admin and is enforced at the database level (i.e., the
  120. appropriate ``UNIQUE`` statements are included in the ``CREATE TABLE``
  121. statement).
  122. .. versionadded:: 1.0
  123. For convenience, unique_together can be a single list when dealing with a single
  124. set of fields::
  125. unique_together = ("driver", "restaurant")
  126. ``verbose_name``
  127. ----------------
  128. .. attribute:: Options.verbose_name
  129. A human-readable name for the object, singular::
  130. verbose_name = "pizza"
  131. If this isn't given, Django will use a munged version of the class name:
  132. ``CamelCase`` becomes ``camel case``.
  133. ``verbose_name_plural``
  134. -----------------------
  135. .. attribute:: Options.verbose_name_plural
  136. The plural name for the object::
  137. verbose_name_plural = "stories"
  138. If this isn't given, Django will use :attr:`~Options.verbose_name` + ``"s"``.