Django meta internal class options

Source: Internet
Author: User

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:

  • Abstract
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


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

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'
  • Db_tablespace

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.
  • Get_latest_by

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.
  • Managed

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.
  • Order_with_respect_to

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.

  • Ordering

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

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.
  • Proxy

This is used to implement the proxy model. I will not talk about it later.
  • Unique_together

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

Verbose_name refers to a more readable name for your model class:

Verbose_name = "pizza"
  • Verbose_name_plural

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'

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.