diff --git a/docs/ref/models/options.txt b/docs/ref/models/options.txt
index 824dae2..4530439 100644
a
|
b
|
Model ``Meta`` options
|
3 | 3 | ====================== |
4 | 4 | |
5 | 5 | This document explains all the possible :ref:`metadata options |
6 | | <meta-options>` that you can give your model in its internal ``class |
7 | | Meta``. |
| 6 | <meta-options>` that you can give your model in its internal |
| 7 | ``class Meta``. |
8 | 8 | |
9 | 9 | Available ``Meta`` options |
10 | 10 | ========================== |
… |
… |
Available ``Meta`` options
|
16 | 16 | |
17 | 17 | .. attribute:: Options.abstract |
18 | 18 | |
19 | | If ``True``, this model will be an :ref:`abstract base class <abstract-base-classes>`. |
| 19 | If ``abstract = True``, this model will be an |
| 20 | :ref:`abstract base class <abstract-base-classes>`. |
20 | 21 | |
21 | 22 | ``app_label`` |
22 | 23 | ------------- |
23 | 24 | |
24 | 25 | .. attribute:: Options.app_label |
25 | 26 | |
26 | | If a model exists outside of the standard :file:`models.py` (for instance, if |
27 | | the app's models are in submodules of ``myapp.models``), the model must define |
28 | | which app it is part of:: |
| 27 | If a model exists outside of the standard :file:`models.py` (for instance, |
| 28 | if the app's models are in submodules of ``myapp.models``), the model must |
| 29 | define which app it is part of:: |
29 | 30 | |
30 | | app_label = 'myapp' |
| 31 | app_label = 'myapp' |
31 | 32 | |
32 | 33 | ``db_table`` |
33 | 34 | ------------ |
34 | 35 | |
35 | 36 | .. attribute:: Options.db_table |
36 | 37 | |
37 | | The name of the database table to use for the model:: |
| 38 | The name of the database table to use for the model:: |
38 | 39 | |
39 | | db_table = 'music_album' |
| 40 | db_table = 'music_album' |
40 | 41 | |
41 | 42 | .. _table-names: |
42 | 43 | |
… |
… |
Table names
|
46 | 47 | To save you time, Django automatically derives the name of the database table |
47 | 48 | from the name of your model class and the app that contains it. A model's |
48 | 49 | database table name is constructed by joining the model's "app label" -- the |
49 | | name you used in ``manage.py startapp`` -- to the model's class name, with an |
50 | | underscore between them. |
| 50 | name you used in :djadmin:`manage.py startapp <startapp>` -- to the model's |
| 51 | class name, with an underscore between them. |
51 | 52 | |
52 | 53 | For example, if you have an app ``bookstore`` (as created by |
53 | 54 | ``manage.py startapp bookstore``), a model defined as ``class Book`` will have |
… |
… |
Django quotes column and table names behind the scenes.
|
65 | 66 | |
66 | 67 | .. attribute:: Options.db_tablespace |
67 | 68 | |
68 | | The name of the database tablespace to use for the model. If the backend doesn't |
69 | | support tablespaces, this option is ignored. |
| 69 | The name of the database tablespace to use for the model. If the backend |
| 70 | doesn't support tablespaces, this option is ignored. |
70 | 71 | |
71 | 72 | ``get_latest_by`` |
72 | 73 | ----------------- |
73 | 74 | |
74 | 75 | .. attribute:: Options.get_latest_by |
75 | 76 | |
76 | | The name of a :class:`DateField` or :class:`DateTimeField` in the model. This |
77 | | specifies the default field to use in your model :class:`Manager`'s |
78 | | :class:`~QuerySet.latest` method. |
| 77 | The name of a :class:`DateField` or :class:`DateTimeField` in the model. |
| 78 | This specifies the default field to use in your model :class:`Manager`'s |
| 79 | :class:`~QuerySet.latest` method. |
79 | 80 | |
80 | | Example:: |
| 81 | Example:: |
81 | 82 | |
82 | | get_latest_by = "order_date" |
| 83 | get_latest_by = "order_date" |
83 | 84 | |
84 | | See the docs for :meth:`~django.db.models.QuerySet.latest` for more. |
| 85 | See the docs for :meth:`~django.db.models.QuerySet.latest` for more. |
85 | 86 | |
86 | 87 | ``managed`` |
87 | | ----------------------- |
| 88 | ----------- |
88 | 89 | |
89 | 90 | .. attribute:: Options.managed |
90 | 91 | |
91 | | Defaults to ``True``, meaning Django will create the appropriate database |
92 | | tables in :djadmin:`syncdb` and remove them as part of a :djadmin:`reset` |
93 | | management command. That is, Django *manages* the database tables' lifecycles. |
| 92 | Defaults to ``True``, meaning Django will create the appropriate database |
| 93 | tables in :djadmin:`syncdb` and remove them as part of a :djadmin:`reset` |
| 94 | management command. That is, Django *manages* the database tables' lifecycles. |
94 | 95 | |
95 | | If ``False``, no database table creation or deletion operations will be |
96 | | performed for this model. This is useful if the model represents an existing |
97 | | table or a database view that has been created by some other means. This is |
98 | | the *only* difference when ``managed`` is ``False``. All other aspects of |
99 | | model handling are exactly the same as normal. This includes |
| 96 | If ``False``, no database table creation or deletion operations will be |
| 97 | performed for this model. This is useful if the model represents an existing |
| 98 | table or a database view that has been created by some other means. This is |
| 99 | the *only* difference when ``managed=False``. All other aspects of |
| 100 | model handling are exactly the same as normal. This includes |
100 | 101 | |
101 | | 1. Adding an automatic primary key field to the model if you don't declare |
102 | | it. To avoid confusion for later code readers, it's recommended to |
103 | | specify all the columns from the database table you are modeling when |
104 | | using unmanaged models. |
| 102 | 1. Adding an automatic primary key field to the model if you don't declare |
| 103 | it. To avoid confusion for later code readers, it's recommended to |
| 104 | specify all the columns from the database table you are modeling when |
| 105 | using unmanaged models. |
105 | 106 | |
106 | | 2. If a model with ``managed=False`` contains a |
107 | | :class:`~django.db.models.ManyToManyField` that points to another |
108 | | unmanaged model, then the intermediate table for the many-to-many join |
109 | | will also not be created. However, the intermediary table between one |
110 | | managed and one unmanaged model *will* be created. |
| 107 | 2. If a model with ``managed=False`` contains a |
| 108 | :class:`~django.db.models.ManyToManyField` that points to another |
| 109 | unmanaged model, then the intermediate table for the many-to-many join |
| 110 | will also not be created. However, the intermediary table between one |
| 111 | managed and one unmanaged model *will* be created. |
111 | 112 | |
112 | | If you need to change this default behavior, create the intermediary |
113 | | table as an explicit model (with ``managed`` set as needed) and use the |
114 | | :attr:`ManyToManyField.through` attribute to make the relation use your |
115 | | custom model. |
| 113 | If you need to change this default behavior, create the intermediary |
| 114 | table as an explicit model (with ``managed`` set as needed) and use the |
| 115 | :attr:`ManyToManyField.through` attribute to make the relation use your |
| 116 | custom model. |
116 | 117 | |
117 | | For tests involving models with ``managed=False``, it's up to you to ensure |
118 | | the correct tables are created as part of the test setup. |
| 118 | For tests involving models with ``managed=False``, it's up to you to ensure |
| 119 | the correct tables are created as part of the test setup. |
119 | 120 | |
120 | | If you're interested in changing the Python-level behavior of a model class, |
121 | | you *could* use ``managed=False`` and create a copy of an existing model. |
122 | | However, there's a better approach for that situation: :ref:`proxy-models`. |
| 121 | If you're interested in changing the Python-level behavior of a model class, |
| 122 | you *could* use ``managed=False`` and create a copy of an existing model. |
| 123 | However, there's a better approach for that situation: :ref:`proxy-models`. |
123 | 124 | |
124 | 125 | ``order_with_respect_to`` |
125 | 126 | ------------------------- |
126 | 127 | |
127 | 128 | .. attribute:: Options.order_with_respect_to |
128 | 129 | |
129 | | Marks this object as "orderable" with respect to the given field. This is almost |
130 | | always used with related objects to allow them to be ordered with respect to a |
131 | | parent object. For example, if an ``Answer`` relates to a ``Question`` object, |
132 | | and a question has more than one answer, and the order of answers matters, you'd |
133 | | do this:: |
| 130 | Marks this object as "orderable" with respect to the given field. This is almost |
| 131 | always used with related objects to allow them to be ordered with respect to a |
| 132 | parent object. For example, if an ``Answer`` relates to a ``Question`` object, |
| 133 | and a question has more than one answer, and the order of answers matters, you'd |
| 134 | do this:: |
134 | 135 | |
135 | | class Answer(models.Model): |
136 | | question = models.ForeignKey(Question) |
137 | | # ... |
| 136 | class Answer(models.Model): |
| 137 | question = models.ForeignKey(Question) |
| 138 | # ... |
138 | 139 | |
139 | | class Meta: |
140 | | order_with_respect_to = 'question' |
| 140 | class Meta: |
| 141 | order_with_respect_to = 'question' |
141 | 142 | |
142 | | When ``order_with_respect_to`` is set, two additional methods are provided to |
143 | | retrieve and to set the order of the related objects: ``get_RELATED_order()`` |
144 | | and ``set_RELATED_order()``, where ``RELATED`` is the lowercased model name. For |
145 | | example, assuming that a ``Question`` object has multiple related ``Answer`` |
146 | | objects, the list returned contains the primary keys of the related ``Answer`` |
147 | | objects:: |
| 143 | When ``order_with_respect_to`` is set, two additional methods are provided to |
| 144 | retrieve and to set the order of the related objects: ``get_RELATED_order()`` |
| 145 | and ``set_RELATED_order()``, where ``RELATED`` is the lowercased model name. For |
| 146 | example, assuming that a ``Question`` object has multiple related ``Answer`` |
| 147 | objects, the list returned contains the primary keys of the related ``Answer`` |
| 148 | objects:: |
148 | 149 | |
149 | | >>> question = Question.objects.get(id=1) |
150 | | >>> question.get_answer_order() |
151 | | [1, 2, 3] |
| 150 | >>> question = Question.objects.get(id=1) |
| 151 | >>> question.get_answer_order() |
| 152 | [1, 2, 3] |
152 | 153 | |
153 | | The order of a ``Question`` object's related ``Answer`` objects can be set by |
154 | | passing in a list of ``Answer`` primary keys:: |
| 154 | The order of a ``Question`` object's related ``Answer`` objects can be set by |
| 155 | passing in a list of ``Answer`` primary keys:: |
155 | 156 | |
156 | | >>> question.set_answer_order([3, 1, 2]) |
| 157 | >>> question.set_answer_order([3, 1, 2]) |
157 | 158 | |
158 | | The related objects also get two methods, ``get_next_in_order()`` and |
159 | | ``get_previous_in_order()``, which can be used to access those objects in their |
160 | | proper order. Assuming the ``Answer`` objects are ordered by ``id``:: |
| 159 | The related objects also get two methods, ``get_next_in_order()`` and |
| 160 | ``get_previous_in_order()``, which can be used to access those objects in their |
| 161 | proper order. Assuming the ``Answer`` objects are ordered by ``id``:: |
161 | 162 | |
162 | | >>> answer = Answer.objects.get(id=2) |
163 | | >>> answer.get_next_in_order() |
164 | | <Answer: 3> |
165 | | >>> answer.get_previous_in_order() |
166 | | <Answer: 1> |
| 163 | >>> answer = Answer.objects.get(id=2) |
| 164 | >>> answer.get_next_in_order() |
| 165 | <Answer: 3> |
| 166 | >>> answer.get_previous_in_order() |
| 167 | <Answer: 1> |
167 | 168 | |
168 | 169 | ``ordering`` |
169 | 170 | ------------ |
170 | 171 | |
171 | 172 | .. attribute:: Options.ordering |
172 | 173 | |
173 | | The default ordering for the object, for use when obtaining lists of objects:: |
| 174 | The default ordering for the object, for use when obtaining lists of objects:: |
174 | 175 | |
175 | | ordering = ['-order_date'] |
| 176 | ordering = ['-order_date'] |
176 | 177 | |
177 | | This is a tuple or list of strings. Each string is a field name with an optional |
178 | | "-" prefix, which indicates descending order. Fields without a leading "-" will |
179 | | be ordered ascending. Use the string "?" to order randomly. |
| 178 | This is a tuple or list of strings. Each string is a field name with an optional |
| 179 | "-" prefix, which indicates descending order. Fields without a leading "-" will |
| 180 | be ordered ascending. Use the string "?" to order randomly. |
180 | 181 | |
181 | | .. note:: |
| 182 | .. note:: |
182 | 183 | |
183 | | Regardless of how many fields are in :attr:`~Options.ordering`, the admin |
184 | | site uses only the first field. |
| 184 | Regardless of how many fields are in :attr:`~Options.ordering`, the admin |
| 185 | site uses only the first field. |
185 | 186 | |
186 | | For example, to order by a ``pub_date`` field ascending, use this:: |
| 187 | For example, to order by a ``pub_date`` field ascending, use this:: |
187 | 188 | |
188 | | ordering = ['pub_date'] |
| 189 | ordering = ['pub_date'] |
189 | 190 | |
190 | | To order by ``pub_date`` descending, use this:: |
| 191 | To order by ``pub_date`` descending, use this:: |
191 | 192 | |
192 | | ordering = ['-pub_date'] |
| 193 | ordering = ['-pub_date'] |
193 | 194 | |
194 | | To order by ``pub_date`` descending, then by ``author`` ascending, use this:: |
| 195 | To order by ``pub_date`` descending, then by ``author`` ascending, use this:: |
195 | 196 | |
196 | | ordering = ['-pub_date', 'author'] |
| 197 | ordering = ['-pub_date', 'author'] |
197 | 198 | |
198 | 199 | ``permissions`` |
199 | 200 | --------------- |
200 | 201 | |
201 | 202 | .. attribute:: Options.permissions |
202 | 203 | |
203 | | Extra permissions to enter into the permissions table when creating this object. |
204 | | Add, delete and change permissions are automatically created for each object |
205 | | that has ``admin`` set. This example specifies an extra permission, |
206 | | ``can_deliver_pizzas``:: |
| 204 | Extra permissions to enter into the permissions table when creating this object. |
| 205 | Add, delete and change permissions are automatically created for each object |
| 206 | that has ``admin`` set. This example specifies an extra permission, |
| 207 | ``can_deliver_pizzas``:: |
207 | 208 | |
208 | | permissions = (("can_deliver_pizzas", "Can deliver pizzas"),) |
| 209 | permissions = (("can_deliver_pizzas", "Can deliver pizzas"),) |
209 | 210 | |
210 | | This is a list or tuple of 2-tuples in the format ``(permission_code, |
211 | | human_readable_permission_name)``. |
| 211 | This is a list or tuple of 2-tuples in the format ``(permission_code, |
| 212 | human_readable_permission_name)``. |
212 | 213 | |
213 | 214 | ``proxy`` |
214 | 215 | --------- |
215 | 216 | |
216 | 217 | .. attribute:: Options.proxy |
217 | 218 | |
218 | | If set to ``True``, a model which subclasses another model will be treated as |
219 | | a :ref:`proxy model <proxy-models>`. |
| 219 | If ``proxy = True``, a model which subclasses another model will be treated as |
| 220 | a :ref:`proxy model <proxy-models>`. |
220 | 221 | |
221 | 222 | ``unique_together`` |
222 | 223 | ------------------- |
223 | 224 | |
224 | 225 | .. attribute:: Options.unique_together |
225 | 226 | |
226 | | Sets of field names that, taken together, must be unique:: |
| 227 | Sets of field names that, taken together, must be unique:: |
227 | 228 | |
228 | | unique_together = (("driver", "restaurant"),) |
| 229 | unique_together = (("driver", "restaurant"),) |
229 | 230 | |
230 | | This is a list of lists of fields that must be unique when considered together. |
231 | | It's used in the Django admin and is enforced at the database level (i.e., the |
232 | | appropriate ``UNIQUE`` statements are included in the ``CREATE TABLE`` |
233 | | statement). |
| 231 | This is a list of lists of fields that must be unique when considered together. |
| 232 | It's used in the Django admin and is enforced at the database level (i.e., the |
| 233 | appropriate ``UNIQUE`` statements are included in the ``CREATE TABLE`` |
| 234 | statement). |
234 | 235 | |
235 | | For convenience, unique_together can be a single list when dealing with a single |
236 | | set of fields:: |
| 236 | For convenience, unique_together can be a single list when dealing with a single |
| 237 | set of fields:: |
237 | 238 | |
238 | | unique_together = ("driver", "restaurant") |
| 239 | unique_together = ("driver", "restaurant") |
239 | 240 | |
240 | 241 | ``verbose_name`` |
241 | 242 | ---------------- |
242 | 243 | |
243 | 244 | .. attribute:: Options.verbose_name |
244 | 245 | |
245 | | A human-readable name for the object, singular:: |
| 246 | A human-readable name for the object, singular:: |
246 | 247 | |
247 | | verbose_name = "pizza" |
| 248 | verbose_name = "pizza" |
248 | 249 | |
249 | | If this isn't given, Django will use a munged version of the class name: |
250 | | ``CamelCase`` becomes ``camel case``. |
| 250 | If this isn't given, Django will use a munged version of the class name: |
| 251 | ``CamelCase`` becomes ``camel case``. |
251 | 252 | |
252 | 253 | ``verbose_name_plural`` |
253 | 254 | ----------------------- |
254 | 255 | |
255 | 256 | .. attribute:: Options.verbose_name_plural |
256 | 257 | |
257 | | The plural name for the object:: |
| 258 | The plural name for the object:: |
258 | 259 | |
259 | | verbose_name_plural = "stories" |
| 260 | verbose_name_plural = "stories" |
260 | 261 | |
261 | | If this isn't given, Django will use :attr:`~Options.verbose_name` + ``"s"``. |
| 262 | If this isn't given, Django will use :attr:`~Options.verbose_name` + ``"s"``. |