Python Django model internal class meta-detailed

Source: Internet
Author: User

The meta of the Django model class is an internal class that defines the behavior characteristics of some Django model classes. Here is a summary of this:

    • Abstract
This property is defined as whether the current model class is not an abstract class. The so-called abstract classes do not correspond to database tables. In general, we use it to generalize some common attribute fields, and then inherit its subclasses to inherit those fields. For example, in the following code, human is an abstract class, the employee is a subclass that inherits the human, the human table is not generated when the SYNCDB command is run, but an employee table is generated that contains the fields inherited from human. Later, if you add a customer model class again, it can also inherit the public properties of human:classHuman(Models.Model):
Name=Models.Charfield(Max_length= -)
Gender_choice=((U' M ',U' Male '),(U' F ',U' Female '),)
Gender=Models.Charfield(Max_length=2,Choices=Gender_choice,Null=True)
classMeta:
Abstract=True
classEmployee(Human):
Joint_date=Models.Datefield()
classCustomer(Human):
First_Name=Models.Charfield(Max_length= -)
Birth_day=Models.Datefield()

The above code, after executing python manage.py syncdb output results in, you can see 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
app_label This option is only used in one case, your model class is not in the models.py file under the default application package, you need to specify that the model class is the application. For example, if you write a model class somewhere else, and this model class belongs to MyApp, then you need to specify:App_label=' MyApp '
    • Db_table
db_table is used to specify a custom database table name. Django has a set of default database table names that generate the data model according to certain rules, and if you want to use a custom table name, specify it by this attribute, such as:Table_name= ' my_owner_table '
    • Db_tablespace
Some databases have database table spaces, such as Oracle. You can use Db_tablespace to specify which database table space the database table for this model is placed in.
    • Get_latest_by
because there is a lastest () method in the Django management approach, it is the most recent line of records. If you have a Datefield or Datetimefield type field in your data model, you can use this option to specify which field the lastest () is selected by.
    • Managed
Since Django automatically generates mapped database tables based on the model class, if you do not want Django to do so, you can set the value of managed to false.
    • Order_with_respect_to
This option is typically used in many-to-many relationships, which point to an associated object. This means that the associated object is sorted after it finds the object. After specifying this property you will get a get_xxx_order () and Set_xxx_order () method through which you can set or go back to the sorted object.
    • Ordering
This field is the field that tells the Django model object which fields are sorted by which record result sets are returned. For example, the following code:Ordering=[' Order_date '] # Sorted by order ascending
Ordering=['-order_date ']# in descending order of orders-= Descending
Ordering=['? order_date ']# random Sort,? Represents a random
    • Permissions
permissions is primarily intended to be used under the Django Admin Management module, and if you set this property you can make the specified method permission description clearer and readable.
    • Proxy
This is to implement the proxy model, which is not described in the following article.
    • Unique_together
unique_together This option is used when you need to remain unique through two fields. For example, suppose you want the combination of a person's FirstName and LastName to be unique, so you need to set this up:Unique_together=(("First_Name","Last_Name"),)
    • Verbose_name
Verbose_name's simple meaning is to give your model class A more readable name:Verbose_name="Pizza"
    • Verbose_name_plural

This option is specified, what is the plural form of the model, for example:

verbose_name_plural = "Stories"

If you do not specify Django will automatically add a ' s ' after the model name

Python model inner class meta-detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.