Metadata Meta

Metadata refers to "all the contents except the field", e.g. sort, database table names, human readable names, and so singular or plural. All of these are non-essential, or even metadata model itself is non-essential.

Increased metadata model, we need to add in a model class subclass name is fixed Meta, and then increase the variety of options in this meta data Meta class below.

class Contract (models.Model):
     "" " Contract " "" 

    Sn = models.CharField (U ' Contract No. ' , MAX_LENGTH = 128, UNIQUE = True) 
    name = models.CharField (U ' contract name ' , 64 = MAX_LENGTH ) 
    Memo = models.TextField (U ' Notes ' , blank = True, null = True) 
    . price = models.IntegerField (U ' contract value ' ) 
    the detail = models.TextField (U ' contract Details ' , blank = True,null=True)
    start_date = models.DateField(blank=True)
    end_date = models.DateField(blank=True)
    license_num = models.IntegerField(u'license数量', blank=True)
    create_date = models.DateField(auto_now_add=True)
    update_date = models.DateField(auto_now=True)

    class Meta:
        verbose_name = '合同'
        verbose_name_plural = "合同"
        ordering = ["create_date"]

    def __str__(self):
        return self.name

 

Each model can have its own metadata classes, each class metadata model where only their own work.

db_table 
specified in the database, table name of the current generated by the model data tables. 
db_tablespace 
custom name of the database table space. 
default_related_name 
By default, setting a model from negative association source model related fields, we use <model_name> _set, which is the source model name + underscore + the SET. 

This metadata option allows you to customize the name of inverse relationship, but also affect the relationship between the reverse lookup name! 

from django.db Import Models 

class Foo (models.Model):
     Pass 

class Bar (models.Model): 
    foo = models.ForeignKey (Foo) 

    class Meta -: 
        default_related_name = ' Bars '    # key here 

ordering 

used for specifying the model generation All objects are sorted, receiving a field name tuple or list thereof. The default ascending order, if coupled with the field name in front of the character " -"It indicates descending order, if the character question mark"? "Denotes a random arrangement. 

Ordering = [ ' pub_date ' ]              # represented by ascending 'pub_date' field arrangement 
Ordering = [ ' -pub_date ' ]             # representation in descending order by 'pub_date' field 
Ordering = [ ' -pub_date ' , ' author ' ]   # means press' pub_date' field in descending order, then `author` field in ascending order. 

unique_together 
is the only joint 

verbose_name 
for setting a visual model object, a human readable name may be, for example, Chinese:.. 
verbose_name = " Story " 
verbose_name = "" 

Verbose_name_plural 
English singular and plural forms. This is a complex model of the object name, such as" apples ". Because we Chinese do not usually distinguish between singular and plural, so keep consistent and verbose_name can.

 

Guess you like

Origin www.cnblogs.com/xiao-apple36/p/11484881.html