Meta options for the Django model

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. The available options generally include the following categories

Abstract

This property is the definition of the current model 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.

Options.abstract
If abstract = True The model is an abstract class

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, and 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 '

Db_table

Db_table is indicated by specifying a custom database. Django has a set of default databases that generate data models according to certain rules.
Options.db_table
Define the table name of the model in the database
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 '

Db_teblespace

Options.db_teblespace
Defines the database table space used by this model. If it is defined in the settin of the project then it will use this value

Get_latest_by

Options.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

Managed

Options.managed
The default value is true, which means that Django can use the SYNCDB and reset commands to create or remove the corresponding database. The default value is True, and if you do not want to do this, 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 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. This is a tuple or list of strings, and no string is a field and is made up of an optional '-' that indicates descending. When there is no '-' in front of the field name, the ascending order is used by default. 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, and if proxy = True, the model is the proxy of its parent

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

Verbose_name the meaning is simple, is to give your model class A more readable name is generally defined as Chinese, we:
Verbose_name = "School"

Verbose_name_plural

This option is specified, what is the plural form of the model, for example:
Verbose_name_plural = "School"
If you do not specify Django will automatically add a ' s ' after the model name

Meta options for the Django model

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.