The meta of the Django model class is an internal class used to define the behavior characteristics of some Django model classes. The following is a summary:
This attribute defines whether the current model class is an abstract class. Abstract classes do not correspond to database tables. Generally, we use it to generalize some public attribute fields, and then its subclass can inherit these fields. For example, in the following code, "human" is an abstract class, and "employee" is a subclass that inherits "human". When running the syncdb command, a human table is not generated, but an employee table is generated, it contains fields inherited from human. If you add another customer model class later, it can also inherit the common attributes of human: class human (models. model ):
Name = models. charfield (max_length = 100)
Gender_choice = (U 'M', u 'male'), (u 'F', u 'female '),)
Gender = models. charfield (max_length = 2, choices = gender_choice, null = true)
Class meta:
Abstract = true
Class employee (human ):
Joint_date = models. datefield ()
Class Customer (human ):
First_name = models. charfield (max_length = 100)
Birth_day = models. datefield ()
The above code runs the output result of Python manage. py syncdb and shows that the human table is not created:
$ Python manage. py syncdb
Creating tables...
Creating table myapp_employee
Creating table myapp_customer
Installing custom SQL...
Installing indexes...
No fixtures found.
App_label is only used in one scenario, that is, your model class is not models under the default application package. in The py file, you need to specify the application that your model class belongs. For example, if you write a model class elsewhere and the model class belongs to MyApp, You need to specify it:
App_label = 'myapp'
Db_table is used to specify the name of a custom database table. Django has a set of default database table names corresponding to the data model generated according to certain rules. If you want to use a custom table name, you can specify this attribute, for example:
Table_name = 'my _ owner_table'
Some databases have database tablespaces, such as oracle. You can use db_tablespace to specify the tablespace in which the database table corresponding to this model is stored.
Since there is a lastest () method in Django's management method, it is to get the latest record. If your data model contains a datefield or datetimefield field, you can use this option to specify which field lastest () is selected.
Since Django will automatically generate a ing database table based on the model class, if you do not want Django to do so, you can set the value of managed to false.
This option is generally used to point to an associated object in a multi-to-many relationship. That is to say, after the correlated object finds this object, it is sorted. After this attribute is specified, you will get a get_xxx_order () and set_xxx_order () methods, through which you can set or go back to the sorted objects.
This field indicates which field the record result set returned by the Django model object is sorted. For example, the following code:
Ordering = ['order _ date'] # sort by order in ascending order
Ordering = ['-order_date'] # sort by order in descending order.-indicates descending order.
Ordering = ['? Order_date '] # random sorting ,? Random
Permissions is mainly used in the Django admin management module. If you set this attribute, the specified method permission description can be clearer and readable.
This is used to implement the proxy model. I will not talk about it later.
The unique_together option is used when two fields need to be unique. For example, if you want that the combination of firstname and lastname of a person must be unique, you need to set it as follows:
Unique_together = ("first_name", "last_name "),)
Verbose_name refers to a more readable name for your model class:
Verbose_name = "pizza"
This option specifies the model's plural form, for example:
Verbose_name_plural = "Stories"
If Django is not specified, An 's' is automatically added after the model name'