63 | | ``db_tablespace`` |
64 | | ----------------- |
| 31 | Available ``Meta`` options |
| 32 | ========================== |
| 33 | |
| 34 | .. currentmodule:: django.db.models |
| 35 | |
| 36 | .. attribute:: Options.abstract |
| 37 | |
| 38 | If ``True``, this model will be an |
| 39 | :ref:`abstract base class <abstract-base-classes>`. |
| 40 | |
| 41 | .. attribute:: Options.app_label |
| 42 | |
| 43 | If a model exists outside of the standard :file:`models.py` (for instance, |
| 44 | if the app's models are in submodules of ``myapp.models``), the model must |
| 45 | define which app it is part of:: |
| 46 | |
| 47 | app_label = 'myapp' |
| 48 | |
| 49 | .. attribute:: Options.db_table |
| 50 | |
| 51 | The name of the database table to use for the model:: |
| 52 | |
| 53 | db_table = 'music_album' |
| 54 | |
93 | | .. versionadded:: 1.1 |
94 | | |
95 | | Defaults to ``True``, meaning Django will create the appropriate database |
96 | | tables in :djadmin:`syncdb` and remove them as part of a :djadmin:`reset` |
97 | | management command. That is, Django *manages* the database tables' lifecycles. |
98 | | |
99 | | If ``False``, no database table creation or deletion operations will be |
100 | | performed for this model. This is useful if the model represents an existing |
101 | | table or a database view that has been created by some other means. This is |
102 | | the *only* difference when ``managed`` is ``False``. All other aspects of |
103 | | model handling are exactly the same as normal. This includes |
104 | | |
105 | | 1. Adding an automatic primary key field to the model if you don't declare |
106 | | it. To avoid confusion for later code readers, it's recommended to |
107 | | specify all the columns from the database table you are modeling when |
108 | | using unmanaged models. |
109 | | |
110 | | 2. If a model with ``managed=False`` contains a |
111 | | :class:`~django.db.models.ManyToManyField` that points to another |
112 | | unmanaged model, then the intermediate table for the many-to-many join |
113 | | will also not be created. However, a the intermediary table between one |
114 | | managed and one unmanaged model *will* be created. |
115 | | |
116 | | If you need to change this default behavior, create the intermediary |
117 | | table as an explicit model (with ``managed`` set as needed) and use the |
118 | | :attr:`ManyToManyField.through` attribute to make the relation use your |
119 | | custom model. |
120 | | |
121 | | For tests involving models with ``managed=False``, it's up to you to ensure |
122 | | the correct tables are created as part of the test setup. |
123 | | |
124 | | If you're interested in changing the Python-level behavior of a model class, |
125 | | you *could* use ``managed=False`` and create a copy of an existing model. |
126 | | However, there's a better approach for that situation: :ref:`proxy-models`. |
127 | | |
128 | | ``order_with_respect_to`` |
129 | | ------------------------- |
| 77 | .. versionadded:: 1.1 |
| 78 | |
| 79 | Defaults to ``True``, meaning Django will create the appropriate database |
| 80 | tables in :djadmin:`syncdb` and remove them as part of a :djadmin:`reset` |
| 81 | management command. That is, Django *manages* the database tables' |
| 82 | lifecycles. |
| 83 | |
| 84 | If ``False``, no database table creation or deletion operations will be |
| 85 | performed for this model. This is useful if the model represents an |
| 86 | existing table or a database view that has been created by some other |
| 87 | means. This is the *only* difference when ``managed=False``. All other |
| 88 | aspects of model handling are exactly the same as normal. This includes: |
| 89 | |
| 90 | 1. Adding an automatic primary key field to the model if you don't |
| 91 | declare it. To avoid confusion for later code readers, it's |
| 92 | recommended to specify all the columns from the database table you |
| 93 | are modeling when using unmanaged models. |
| 94 | |
| 95 | 2. If a model with ``managed=False`` contains a |
| 96 | :class:`~django.db.models.ManyToManyField` that points to another |
| 97 | unmanaged model, then the intermediate table for the many-to-many |
| 98 | join will also not be created. However, a the intermediary table |
| 99 | between one managed and one unmanaged model *will* be created. |
| 100 | |
| 101 | If you need to change this default behavior, create the intermediary |
| 102 | table as an explicit model (with ``managed`` set as needed) and use |
| 103 | the :attr:`~ManyToManyField.through` attribute to make the relation |
| 104 | use your custom model. |
| 105 | |
| 106 | For tests involving models with ``managed=False``, it's up to you to ensure |
| 107 | the correct tables are created as part of the test setup. |
| 108 | |
| 109 | If you're interested in changing the Python-level behavior of a model class, |
| 110 | you *could* use ``managed=False`` and create a copy of an existing model. |
| 111 | However, there's a better approach for that situation: :ref:`proxy-models`. |
133 | | Marks this object as "orderable" with respect to the given field. This is almost |
134 | | always used with related objects to allow them to be ordered with respect to a |
135 | | parent object. For example, if an ``Answer`` relates to a ``Question`` object, |
136 | | and a question has more than one answer, and the order of answers matters, you'd |
137 | | do this:: |
138 | | |
139 | | class Answer(models.Model): |
140 | | question = models.ForeignKey(Question) |
141 | | # ... |
142 | | |
143 | | class Meta: |
144 | | order_with_respect_to = 'question' |
145 | | |
146 | | When ``order_with_respect_to`` is set, two additional methods are provided to |
147 | | retrieve and to set the order of the related objects: ``get_RELATED_order()`` |
148 | | and ``set_RELATED_order()``, where ``RELATED`` is the lowercased model name. For |
149 | | example, assuming that a ``Question`` object has multiple related ``Answer`` |
150 | | objects, the list returned contains the primary keys of the related ``Answer`` |
151 | | objects:: |
152 | | |
153 | | >>> question = Question.objects.get(id=1) |
154 | | >>> question.get_answer_order() |
155 | | [1, 2, 3] |
156 | | |
157 | | The order of a ``Question`` object's related ``Answer`` objects can be set by |
158 | | passing in a list of ``Answer`` primary keys:: |
159 | | |
160 | | >>> question.set_answer_order([3, 1, 2]) |
161 | | |
162 | | The related objects also get two methods, ``get_next_in_order()`` and |
163 | | ``get_previous_in_order()``, which can be used to access those objects in their |
164 | | proper order. Assuming the ``Answer`` objects are ordered by ``id``:: |
165 | | |
166 | | >>> answer = Answer.objects.get(id=2) |
167 | | >>> answer.get_next_in_order() |
168 | | <Answer: 3> |
169 | | >>> answer.get_previous_in_order() |
170 | | <Answer: 1> |
171 | | |
172 | | ``ordering`` |
173 | | ------------ |
| 115 | Marks this object as "orderable" with respect to the given field. This is |
| 116 | almost always used with related objects to allow them to be ordered with |
| 117 | respect to a parent object. For example, if an ``Answer`` relates to a |
| 118 | ``Question`` object, and a question has more than one answer, and the order |
| 119 | of answers matters, you'd do this:: |
| 120 | |
| 121 | class Answer(models.Model): |
| 122 | question = models.ForeignKey(Question) |
| 123 | # ... |
| 124 | |
| 125 | class Meta: |
| 126 | order_with_respect_to = 'question' |
202 | | ``permissions`` |
203 | | --------------- |
| 158 | When ``order_with_respect_to`` is set, two additional methods are provided |
| 159 | to retrieve and to set the order of the related objects: |
| 160 | ``get_RELATED_order()`` and ``set_RELATED_order()``, where ``RELATED`` is |
| 161 | the lowercased model name. For example, assuming that a ``Question`` object |
| 162 | has multiple related ``Answer`` objects, the list returned contains the |
| 163 | primary keys of the related ``Answer`` objects:: |
| 164 | |
| 165 | >>> question = Question.objects.get(id=1) |
| 166 | >>> question.get_answer_order() |
| 167 | [1, 2, 3] |
| 168 | |
| 169 | The order of a ``Question`` object's related ``Answer`` objects can be set |
| 170 | by passing in a list of ``Answer`` primary keys:: |
| 171 | |
| 172 | >>> question.set_answer_order([3, 1, 2]) |
| 173 | |
| 174 | The related objects also get two methods, ``get_next_in_order()`` and |
| 175 | ``get_previous_in_order()``, which can be used to access those objects in |
| 176 | their proper order. Assuming the ``Answer`` objects are ordered by ``id``:: |
| 177 | |
| 178 | >>> answer = Answer.objects.get(id=2) |
| 179 | >>> answer.get_next_in_order() |
| 180 | <Answer: 3> |
| 181 | >>> answer.get_previous_in_order() |
| 182 | <Answer: 1> |
269 | | If this isn't given, Django will use :attr:`~Options.verbose_name` + ``"s"``. |
| 236 | If this isn't given, Django will use :attr:`~Options.verbose_name` + ``"s"``. |
| 237 | |
| 238 | |
| 239 | Meta information at runtime |
| 240 | =========================== |
| 241 | |
| 242 | Model instances have a ``_meta`` attribute, which is an instance of |
| 243 | :class:`django.db.models.Options`. Any of the properties listed above are |
| 244 | available on ``_meta`` as well as those listed below. |
| 245 | |
| 246 | .. attribute:: Options.auto_field |
| 247 | |
| 248 | If the model has an :class:`~django.db.models.AutoField`, this property |
| 249 | will return that field. Otherwise, this property will be undefined. |
| 250 | Check :attr:`Options.has_auto_field` before trying to access. |
| 251 | |
| 252 | .. attribute:: Options.has_auto_field |
| 253 | |
| 254 | ``True`` if this model has an :class:`~django.db.models.AutoField`. If |
| 255 | ``True``, :attr:`Options.auto_field` returns this field. |
| 256 | |
| 257 | .. attribute:: Options.pk |
| 258 | |
| 259 | Returns the :attr:`~Field.primary_key` field for this Model, either one |
| 260 | explicitly defined or the generated :class:`~django.db.models.AutoField`. |
| 261 | |
| 262 | .. method:: Options.get_field(name, many_to_many=True) |
| 263 | |
| 264 | Returns the field with the given name. If the field does not exist for |
| 265 | this model, raises :exc:`~django.db.models.fields.FieldDoesNotExist`. |