django model inner classes meta explanation

meta django model class is an internal class, he used some of the behavioral characteristics django model.

Built by a class "class Meta" to your custom metadata model

Any data is not a field: model metadata is

 

 

class UserInfo(AbstractUser):
    '''
    用户表
    '''

    nid = models.AutoField(primary_key=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now=True)
    telphone = models.CharField(max_length=11,null=True,unique=True)
    # avater = models.FileField(upload_to='avater/',dafult='avatars/defualt.png')
    blog = models.OneToOneField(to='Blog',to_field='nid',null=True,on_delete=models.CASCADE)

    class Meta:
        verbose_name Verbose_name_user = = ' user table ' 
        named db_table, = ' User '

 

1. app_label

app_label This option is used only in one case, is models.py files in your model class is not in the default application package, this time you need to specify the model classes that application. For example, you write a model in other parts of the class, and this model belongs to the class myapp, then you need to specify which is as follows:

app_label='myapp'

2. db_table

db_table is used to specify a custom database table names. Django models have a default data to generate the corresponding database table names according to certain rules, if you want to use from the table name defined by the designation of this property, such as

table_name = 'user'

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

If your table name is a SQL reserved word, or contains characters that are not allowed in Python variable names - notably the character - it does not matter Django automatically for you behind the scenes of the column name and table name in quotation marks.

 

3. db_tablespace

Some databases have a database table space, such as Oracle.

You can specify the model corresponding database table on which database table space by db_tablespace.

 

4. get_latest_by

Because Django management methods There is a lastest () method, is to get a row recently. If you have a data model DateField or DateTimeField type of field, you can specify the lastest by this option () is selected according to which field.

One name DateField or DateTimeField field if this option is provided, the module will have a get_latest () function to get the "latest" objects (according to the field):

get_latest_by = "order_date"

 

5.managed

Because Django model classes are automatically generated based on the mapping database tables, if you do not want to Django, you can set the value managed to False.

The default value is True, this option Django or migrations can migrate the database table is True, or delete operation. At this time Django will manage the life cycle of the tables in the database

If False, it does not perform database table to create, delete and other operations. Can be used in existing tables, database views and the like, other operations are the same.

 

6.order_with_respect_to

This option is generally used in many relationships, it points to an associated object. After the object is to find the object that is associated with it is sorted. After specifying this property you will get a get_XXX_order () and set_XXX_order () method by which you can set up or go back to sort of the object.

For example, if a PizzaToppping Pizza associated with a target, to do so:

order_with_respect_to = 'pizza'

 

7. ordering

This field is to tell Django model objects returned result set of records of which field is sorted. For example the following code:

= Ordering [ ' ORDER_DATE ' ] 
 # Press ascending order 
Ordering = [ ' -order_date ' ] 
 # Press descending order, - for descending 
Ordering = [ ' ORDER_DATE? ' ] 
 # random order,? Denotes a random 
Ordering = [ ' -pub_date ' , ' author ' ]
 # for pub_date descending and ascending to author

Note that: no matter how many fields you use to sort, admin uses only the first field

 

8. permissions

permissions primarily for use in Django Admin management module, if you set this property allows the specified method permissions described in more legible.

To create an object require additional rights if an object has admin set, each object add, delete and change permissions are automatically created man (according to the option) The following example specifies an additional permission:. Can_deliver_pizzas:

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

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

 

9.unique_together

unique_together this option: Use when you need to keep the uniqueness of the two fields.

This would make restrictions (that is, related UNIQUE statements are included in the CREATE TABLE statement) while the Django admin and database tiers. For example: a combination of both FirstName and LastName of a Person must be unique, you need to set:

unique_together = (("first_name", "last_name"),)

 

10.verbose_name

verbose_name meaning is very simple, is to give your model classes from a more readable name:

= = verbose_name_user the verbose_name ' user table '

If you do not provide this option, Django will use munged version of a class name instead: CamelCase becomes camel case.

 

11.verbose_name_plural

This option is what the plural form of the model is specified, such as:

 

verbose_name_plural = "stories"

If you do not provide this option, Django will use verbose_name + "s".

 

12.abstract

     This property is defined in the current model class is not an abstract class. The so-called abstract class is not appropriate database tables. Generally, we use it to summarize some public property field, and then inherit its subclasses can inherit these fields.

 

Human example, the following code is an abstract class. Employee is a subclass inherits Human, then the implementation of syncdb command does not generate a Human table. But it will generate an Employee table, which includes Human inherited in the field. Suppose a Customer added later model class, which can be the same public properties inherited 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, after running the output into the python manage.py syncdb. Human table can be seen and has not been created:

$ python manage.py syncdb
Creating tables ...
Creating table myapp_employee
Creating table myapp_customer
Installing custom SQL ...
Installing indexes ...
No fixtures found.

 

Guess you like

Origin www.cnblogs.com/jinfanfu/p/11466127.html