Model fields and field parameters

A, ORM Introduction

1, concepts: ORM (Object Relational Mappingt), object-relational mapping

The mapping between classes and database: 2, in essence,

3. Advantages:

  Developers do not write database

4. Cons:

  Developers, database skills loss

  Class -> SQL statements, will take time, reduce efficiency

Second, field

1, common field

a、AutoField

int increment, must fill in the fields parameter primary_key = True, if not this field is automatically created id column

b、IntegerField

Integer type, range: ~ -2147483648 2147483647

c、CharField

varchar (), the parameter field must be filled max_length

d、DateField

Date, datatime module datatime.data ()

字段:auto_now 和auto_now_add

Time Format: YYYY-MM- DD

and, DataTimeField

Time Format: YYYY-MM- DD HH: the MM [: SS [.uuuuuu]] [the TZ] , datatime.datatime

Unimportant field


    BigAutoField (AutoField)
         - BIGINT auto-increment, you must fill parameter = primary_key True
 
    SmallIntegerField (IntegerField):
         - 32767 ~ small integer -32768 

    PositiveSmallIntegerField (PositiveIntegerRelDbTypeMixin, IntegerField)
         - a positive integer smaller 32767 ~ 0 
PositiveIntegerField (PositiveIntegerRelDbTypeMixin, IntegerField) - n ~ 2147483647 integer of 0 BigIntegerField (IntegerField): - long integer (signed) -9223372036854775808 ~ 9223372036854775807 BooleanField (Field,) - Boolean type NullBooleanField (Field,): - can be null Boolean the TextField (Field,) - text type EmailField (as CharField): - a string type, Django Admin and authentication mechanisms provided ModelForm IPAddressField (Field,) - string type, Django Admin IPV4 and authentication mechanisms provided ModelForm GenericIPAddressField (Field,) - string type, Django Admin provides authentication and ModelForm Ipv4 and Ipv6 - parameters: Protocol, specifies Ipv4 or Ipv6, ' both- ' , " ipv4 " , " ipv6 " unpack_ipv4, if you specify True, the input ffff ::: 192.0.2.1 when resolves to 192.0.2.1, turn on this feature, you need to = Protocol " both- " URLField (CharField) - string type, Django Admin ModelForm provide authentication and the URL SlugField (as CharField) -String type, Django ModelForm the Admin provides authentication and support letters, numbers, underscore, hyphen (minus) CommaSeparatedIntegerField (as CharField) - string type, must be a comma-delimited format number UUIDField (Field,) - string type, Django Admin ModelForm and provides verification of the UUID format that FilePathField will (Field,) - string, Django Admin provides functions to read and ModelForm folder file - parameters: path, folder path match = None, regular matching recursive This = False, the following recursive folder allow_files = True, allows files allow_folders = False, allows folder FileField (Field,) - string path stored in the database, upload files to a specified directory - parameters: upload_to = "" path to save the uploaded file Storage = None storage component, the default django.core.files.storage.FileSystemStorage ImageField (FileField) - string path stored in the database, upload files to a specified directory - parameters: upload_to = "" path to save the uploaded file storage = None storage component, the default django.core.files.storage.FileSystemStorage width_field = None, highly upload pictures stored in the database field name (string) height_field =Saved width None Photo database field names (character string) TimeField (DateTimeCheckMixin, Field,) - time format HH: the MM [: SS [.uuuuuu]] DurationField (Field,) - long integer interval stored in the database in accordance with bigint , ORM is acquired datetime.timedelta type FloatField (Field,) - float DecimalField (Field,) - 10 binary decimal - parameters: max_digits, fractional total length decimal_places, fractional bit length BinaryField (Field,) - binary type

Custom field of type char

class FixedCharField (models.Field):
     DEF  the __init__ (Self, MAX_LENGTH, args *, ** kwargs): 
        . Super () the __init__ (MAX_LENGTH = MAX_LENGTH, args *, ** kwargs) 
        self.length = MAX_LENGTH 

    DEF db_type (Self, Connection):
         # field types generated database table defined as char, length length value specified 
        return  ' char (% S) ' % self.length 


class class (models.Model):
     # using a custom field type char 
    name = FixedCharField (max_length = 25)

Third, the field parameter

1、null

Null, null = True

2、unique

The only index contains unique = True

3、db_index

Index containing index: db_index = True

4、default

Set Default

5, the difference auto_now_add and auto_now

a, can not both be True

b, time after auto_now_add update data creation

Update time c, atuo_now modify data

Note: DateField and DateTimeField auto_now_add parameter field has no need to provide specific value

Fourth, the relationship field

1, the foreign key (typically a company will not be used)

Fields: ForeignKey

Field parameters

a、to

Set the table to be associated

b, to_field (generally do not)

To set the associated table field, and to be previously used

c、db_constraint

Set soft constraints, db_constraint = False, the average company without foreign keys, if also soft constraints

d、on_delete

When you delete data associated table, the current table row behavior associated with it. 
models.CASCADE 
delete the associated data, delete the associated 
models.DO_NOTHING 
delete the associated data, causing errors IntegrityError 
models.PROTECT 
delete the associated data, causing errors ProtectedError 
models.SET_NULL 
delete the associated data, associated value is set to null (FK premise field needs to be set to empty) 
models.SET_DEFAULT 
delete the associated data, the value associated with the default value (FK fields provided to set default values) 
models.SET 
associated delete data, 
a. value associated with the specified value provided: models.SET (value) 
. B value associated executable object set as return value is provided: models.SET (executable object)

Note: Django2 write on_delete = models.CASCADE

2, one on one

Two tables into one table, the advantages of the query frequency sub-table

Fields: OneToOneField

parameter:

a、to

b、to_field

c、on_delete

3-many

Fields: ManyToManyField

Field parameters: to

Fifth, the meta-information

Field metaclasses: Meta

index_together

Joint index

unique_together

United unique index

Guess you like

Origin www.cnblogs.com/wt7018/p/11261282.html