Define metadata for a model through an inner class “class Meta”, like this:
| |
Model metadata means: any data that is not a field
such as ordering options, admin options, and so on.
Below are all the Meta options you might use. None of them is required. Whether to add class Meta to a model is entirely optional.
app_label
The app_label option is used in only one situation: your model class is not in the models.py file of the default application package, and you need to specify which application the model class belongs to. For example, if you write a model class somewhere else and that model class belongs to myapp, then at this point you need to specify it as:app_label='myapp'
db_table
db_table is used to specify a custom database table name. Django has a set of defaults that generate the database table name corresponding to a data model according to certain rules. If you want to use a custom table name, specify it through this attribute, for example:table_name='my_owner_table'
If this parameter is not provided, Django uses app_label + '_' + module_name as the table name.
If your table name is an SQL reserved word, or contains characters that Python variable names do not allow – especially hyphens – that is fine. Django will automatically quote the column and table names for you behind the scenes.
db_tablespace
Some databases have database tablespaces, such as Oracle. You can use db_tablespace to specify which database tablespace the database table corresponding to this model goes into.
get_latest_by
Because Django’s management methods include a latest() method, which gets the most recent record. If your data model has a field of type DateField or DateTimeField, you can use this option to specify which field latest() selects by.
The name of a DateField or DateTimeField field. If this option is provided, the module will have a get_latest() function to get the “latest” object (according to that field):
get_latest_by = "order_date"
managed
Because Django automatically generates the mapped database table from the model class, if you do not want Django to do this, you can set the value of managed to False.
The default value is True. When this option is True, Django can perform migrate or migrations, delete, and other operations on the database table. At this point Django will manage the lifecycle of the table in the database
When it is False, no create, delete, or other operations are performed on the database table. It can be used for existing tables, database views, and so on; other operations are the same.
order_with_respect_to
This option is generally used in many-to-many relationships, and it points to an associated object. That is to say, once the associated object finds this object, it is sorted. After specifying this attribute you get a get_XXX_order() and set_XXX_order() method, through which you can set or get the sorted objects.
For example, if a PizzaTopping is associated with a Pizza object, do this:
order_with_respect_to = 'pizza'
This field tells Django which field the record result set returned by the model object is sorted by. For example, the following code:
ordering=['order_date']
Sort by order ascending
ordering=['-order_date']
Sort by order descending; - means descending
ordering=['?order_date']
Random order; ? means random
ordering = ['-pub_date', 'author']
Sort by pub_date descending, then by author ascending
Note: no matter how many fields you use for sorting, admin only uses the first field
permissions
permissions is mainly for use under the Django Admin management module. If you set this attribute, the permission descriptions of the specified methods become clearer and more readable.
The extra permissions required to create an object. If an object has admin settings, then the add, delete, and change permissions of each object are created automatically (based on this option). The following example specifies one additional permission: can_deliver_pizzas:
permissions = (("can_deliver_pizzas", "Can deliver pizzas"),)
This is a tuple or list of 2-element tuples, where the format of the 2-element tuple is: (permission_code, human_readable_permission_name).
unique_together
The unique_together option is used when: you need to keep uniqueness across two fields. This enforces the constraint both at the Django admin layer and at the database layer (that is, the relevant UNIQUE statement is included in the CREATE TABLE statement). For example: the combination of a Person’s FirstName and LastName must be unique, so you need to set it like this:
unique_together = (("first_name", "last_name"),)
verbose_name
verbose_name means simply giving your model class a more readable name:
verbose_name = "pizza"
If this option is not provided, Django substitutes a munged version of the class name: CamelCase becomes camel case.
verbose_name_plural
This option specifies what the plural form of the model is, for example:
verbose_name_plural = "stories"
If this option is not provided, Django uses verbose_name + “s”.
