The class Meta details in the Django "26th" Django model

Source: Internet
Author: User

class Foo(models.Model): bar = models.CharField(maxlength=30) class Meta: # ...

The Model metadata is "any data that is not a field"-such as sorting options, admin options, and so on.

Here are all the possible Meta options. None of the options are required. Whether to add Class Meta to your model is completely optional.

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‘   

If this argument is not provided, Django uses App_label + ' _ ' + module_name as the name of the table.

If your table's name is a SQL reserved word, or contains characters that are not allowed by the Python variable name-especially hyphens-it doesn't matter. Django will automatically enclose your column names and table names in quotation marks behind the scenes.

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.

The name of a Datefield or Datetimefield field. If this option is provided, the module will have a get_latest () function to get the "most recent" object (depending on the field):

= "order_date"
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.

The default value is true, and when this option is true, Django can migrate or migrations the database table, delete, and so on. At this time Django will manage the life cycle of the tables in the database

If False, the database table is not created, deleted, and so on. Can be used for existing tables, database views, and so on, other operations are the same.

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.

For example, if a pizzatoppping is associated to a Pizza object, do this:

= ‘pizza‘

... Allows toppings to be sorted according to the relevant pizza.

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‘] # 按订单升序排列ordering=[‘-order_date‘] # 按订单降序排列,-表示降序ordering=[‘?order_date‘] # 随机排序,?表示随机ordering = [‘-pub_date‘, ‘author‘]# 对 pub_date 降序,然后对 author 升序

Note that no matter how many fields you use, Admin uses only the first field

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.

The additional permissions that are required to create an object. If an object has the admin setting, then each object's add, delete, and change permissions are automatically created by the person (depending on the option). The following example specifies an additional permission: Can_deliver_pizzas:

= (("can_deliver_pizzas", "Can deliver pizzas"),)

This is a tuple or list of 2-element tuple, where 22-element tuple is in the format: (Permission_code, Human_readable_permission_name).

Unique_together

Unique_together This option is used when you need to remain unique through two fields. This can be done at the same time as the Django admin layer and the database layer (i.e. the associated UNIQUE statement is included in the CREATE TABLE statement). For example: The combination of a person's FirstName and LastName must be unique, so you need to set this up:

= (("first_name", "last_name"),)
Verbose_name

Verbose_name's simple meaning is to give your model class A more readable name:

= "pizza"

If this option is not available, Django replaces it with a munged version of the class name: CamelCase becomes camel case .

Verbose_name_plural

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

= "stories"

If this option is not available, Django will use Verbose_name + "s".

The class Meta details in the Django "26th" 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.