Django meta Option

Source: Internet
Author: User

Define metadata for your model using an embedded class "class meta", similar to the following:

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 and Admin options.

Below are all the meta options that may be used. None of them are required. It is optional to add class Meta to your model.

The name of the table corresponding to this module in the database:

Db_table = "pizza_orders"

If this parameter is not provided, Django uses app_label + '_' + module_name as the table name.

It doesn't matter if your table name is an SQL reserved word or contains characters that are not allowed by the python variable name, especially hyphens. django automatically quotes column names and table names behind the scenes.

The name of a datefield or datetimefield. If this option is provided, the module will have a get_latest () function to get the "latest" Object (based on that field ):

Get_latest_by = "order_date"

The object marking the given field is "orderable. it is used to associate objects to allow them to sort in the way the parent object expects. for example, if a pizzatoppping is associated with a pizza object, do the following:

Order_with_respect_to = 'pizza'

... Toppings can be sorted by related pizza.

Mark this object as sorted by given fields. this is almost always used with related objects to allow them to be ordered with respect to a parent object. for example, if a pizzatoppping is associated with a pizza object, you can use:

Order_with_respect_to = 'pizza'

This allows toppings to be ordered to be sorted about related pizza objects.

The default sorting field and sorting method are used to obtain an object list in any scenario:

Ordering = ['-order_date']

This is a tuple or a string list. each string is a field name and an optional prefix "-". This prefix indicates descending order (descending ). if this prefix is not available, it indicates sorting in ascending order. string "? "Indicates random sorting.

For example, to sort the pub_date field in ascending order, do the following:

Ordering = ['pub _ date']

Sort them in descending order, as shown in the following code:

Ordering = ['-pub_date']

To sort pub_date in descending order and then author in ascending order, as shown in the following code:

Ordering = ['-pub_date', 'author']

Note that, no matter how many fields are used for sorting, admin only uses the first field.

The additional permissions required to create an object. if an object has admin settings, the add, delete, and change permissions of each object will be automatically created by the person (based on this option. the following example specifies an additional permission: can_deliver_pizzas:

Permissions = ("can_deliver_pizzas", "can deliver pizzas "),)

This is a tuple or list of 2-element tuple. The format of the two-element tuple is: (permission_code, human_readable_permission_name )..

Sets of field names that, taken together, must be unique:

Unique_together = ("driver", "restaurant "),)

This is a list of fields. The comprehensive values of these fields must be unique. this restricts both the Django admin layer and the database layer (that is, the related unique statements will be included in the create table statement ).

Is a unique name of the object with better readability:

Verbose_name = "pizza"

If this option is not provided, Django uses the munged version of the class name instead: camelcase becomes camel case.

The complex number of object names:

Verbose_name_plural = "Stories"

If this option is not provided, Django uses verbose_name + "S ".

The rest of this has not yet been edited
Table names
Automatic Primary Key Fields

Original 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: 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.