Django Model meta Options

Source: Internet
Author: User

Reference: https://www.cnblogs.com/flash55/p/6265405.html

The meta of the Django model class is an inner class that defines the behavior characteristics of some Django model classes.

1) Abstract: This attribute defines whether the current model is an abstract class, and so-called abstract classes do not correspond to database tables. Generally we use it to generalize some common attribute fields, and then inherit its subclasses to inherit the fields. Abstact = True This model is an abstract class

2) App_label: This type of selection is used only in one case, that is, your model is not in the models.py file under the default application package, you need to specify which application your model is.

Options.app_label
If a model is defined in the default models.py, for example if your app's models is under the Myapp.models submodule, you must define App_label to let Django know which app it belongs to.
App_label = ' MyApp '

3) Base_manager_name: Specify the name of the managers

4) db_table is the name of the specified custom database table, and the name of the table in the database that defines the model
db_table = ' Students '
If you want to use a custom table name, you can do this by following this property
table_name = ' my_owner_table '

5) Db_tablespace: Defines the database table space used by this model.

6) Default_manger_name, specify the name of the manager.

7) Default_related_name: Specifies the name of the model in reverse association, it should be noted that this name should be unique, it is recommended to include the name of the app and model names to avoid conflicts %(app_label)s和%(model_name)s .

8) Get_latest_by: Specify a Datefield or Datetimefield in the model. This setting allows you to sort by default using the specified field when using the lastest method on the model's manager

9) Manage the default value is true, which means that Django can use the SYNCDB and reset commands to create or remove the corresponding database, and if you do not want to do so, you can set the value of manage to False

Order_with_respect_to This option is typically used in many-to-many relationships, which point to an associated object, meaning that the associated object is sorted after it has been found, specifying this property will give you a Gget_xxx_order () and Set_xxx_ The order () method through which you can set or get the sorted object

Ordering: This is the field that tells the Django model object which field to sort the result set of records returned by. This is the Ganso or list of a string, each of which is a field and is made up of an optional '-' in descending order. When there is no '-' in front of the field name, The default is to use ascending order. Use '? ' will be randomly arranged

    • ordering=[' order_date ' # sorted in ascending order
    • ordering=['-order_date ' # sorted in descending order,-= Descending
    • ordering=['? Order_date '] # random sort,? Represents a random
    • ordering=['-pub_date ', ' author '] # in descending order of pub_date, in ascending order of author

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. Django automatically creates add, delete, and modify permissions for each object that has the admin set.
Permissions = ((' Can_deliver_pizzas ', ' can deliver pizzas '))

Proxy: This is to implement the proxy model, if Proxy = True, indicates that the model is its parent's proxy model

) required_db_features :

15)required_db_vendor:指定用那种数据库

Select_on_save: Specifies which algorithm to use to save the data

Indexes: Refer to this 78916371






]

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"),)
A Manytomanyfield cannot be included in the Unique_together. If you need to verify the unique validation associated to the Manytomanyfield field, try using signal (signal) or explicitly specifying the through property.

Verbose_name: Create an easy-to-understand name for the model, singular.

Verbose_name_plural: Unlike the above, it is a plural form.

Django Model meta Options

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.