Fields and field parameters of the model

Introduction to ORM 1. Concept: ORM (Object Relational Mapping), Object Relational Mapping 2. Essence: mapping between classes and databases 3. Advant...

Introduction to ORM

1. Concept: ORM (Object Relational Mapping), Object Relational Mapping

2. Essence: mapping between classes and databases

3. Advantages:

Developers do not need to write databases

4. Disadvantages:

Developers, database skills lost

Class - > SQL statement, takes time, reduces efficiency

II. FIELDS

1. Common fields

a,AutoField

The int increases by itself, and the field parameter primary_key=True must be filled in. If there is no field, the column of id will be created automatically.

b,IntegerField

Integer type, range: - 2147483648 - 2147483647

c,CharField

varchar(), you must fill in the field parameter max_length

d,DateField

Date. data () in the datatime module

Fields: auto_now and auto_now_add

Time format: YYYY-MM-DD

e,DataTimeField

Time format: YYYY-MM-DDDHH: MM [: SS [. uuuuuu] [TZ], datatime.datatime

Unimportant fields

BigAutoField(AutoField) - bigint Self-adding column, must fill in parameters primary_key=True SmallIntegerField(IntegerField): - Decimal integer -32768 ~ 32767 PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField) - Positive decimal integer 0 ~ 32767
PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField) - Positive integer 0 ~ 2147483647 BigIntegerField(IntegerField): - Long shaping(Symbolic) -9223372036854775808 ~ 9223372036854775807 BooleanField(Field) - Boolean value type NullBooleanField(Field): - Boolean values that can be empty TextField(Field) - Text type EmailField(CharField): - String type, Django Admin as well as ModelForm Provide verification mechanism in IPAddressField(Field) - String type, Django Admin as well as ModelForm Provide validation in IPV4 mechanism GenericIPAddressField(Field) - String type, Django Admin as well as ModelForm Provide validation in Ipv4 and Ipv6 - Parameters: protocol,Used for specifying Ipv4 or Ipv6, 'both',"ipv4","ipv6" unpack_ipv4, If specified as True,Then enter::ffff:192.0.2.1 It can be resolved to 192..0.2.1,To turn this on, you need to protocol="both" URLField(CharField) - String type, Django Admin as well as ModelForm Provide validation in URL SlugField(CharField) - String type, Django Admin as well as ModelForm Provide validation support for letters, numbers, underscores, connectors (minus sign) CommaSeparatedIntegerField(CharField) - String type, format must be comma-separated numbers UUIDField(Field) - String type, Django Admin as well as ModelForm Provided in the UUID Verification of format FilePathField(Field) - Character string, Django Admin as well as ModelForm Provides the ability to read files under folders - Parameters: path, Folder Path match=None, regular expression matching recursive=False, Recursive Folder allow_files=True, Permissible files allow_folders=False, Allow folders FileField(Field) - String, path saved in database, file uploaded to specified directory - Parameters: upload_to = "" Save path of uploaded file storage = None Storage component, default django.core.files.storage.FileSystemStorage ImageField(FileField) - String, path saved in database, file uploaded to specified directory - Parameters: upload_to = "" Save path of uploaded file storage = None Storage component, default django.core.files.storage.FileSystemStorage width_field=None, Highly saved database field name (string) of uploaded image height_field=None The width of the uploaded image saves the name of the database field (string) TimeField(DateTimeCheckMixin, Field) - Time format HH:MM[:ss[.uuuuuu]] DurationField(Field) - Long integer, time interval, database according to bigint Storage, ORM The value obtained in the datetime.timedelta type FloatField(Field) - float DecimalField(Field) - 10 Decimal - Parameters: max_digits,Total decimal length decimal_places,Decimal length BinaryField(Field) - Binary type

Fields of custom char type

class FixedCharField(models.Field): def __init__(self, max_length, *args, **kwargs): super().__init__(max_length=max_length, *args, **kwargs) self.length = max_length def db_type(self, connection): # Restrict the field type of the generated database table to char,Count Reg length The specified value return 'char(%s)' % self.length class Class(models.Model): # Use custom char Type field name = FixedCharField(max_length=25)

III. FIELD PARAMETERS

1,null

null=True

2,unique

Unique index with unique=True

3,db_index

Index, with index: db_index=True

4,default

Setting default values

5. The difference between auto_now_add and auto_now

a. Can't be True at the same time

b. Time update after auto_now_add data creation

c, atuo_now. Time update of data modification

Note: The DateField and DateTimeField fields have auto_now_add parameters, and no specific values need to be set.

IV. RELATIONAL FIELDS

1. Foreign keys (not commonly used by companies)

Field: ForeignKey

Field parameters

a,to

Setting tables to be associated

b, to_field (generally not used)

Set the fields to be associated with the table, which can be used before to

c,db_constraint

Setting soft constraints, db_constraint=False, generally companies do not use foreign keys, if used is also soft constraints

d,on_delete

When data in an associated table is deleted, the behavior of the current table and its associated rows. models.CASCADE Delete associated data and delete associated data models.DO_NOTHING Delete the associated data and cause an error Integrity Error models.PROTECT Delete the associated data and cause an error Protected Error models.SET_NULL Delete the associated data and set the associated value to null (assuming that the FK field needs to be set to null) models.SET_DEFAULT Delete the associated data and set the associated value to the default value (assuming that the default value is set for the FK field) models.SET Delete the associated data, a. The value associated with it is set to the specified value, set: models. SET (value) b. The value associated with it is set to the return value of the executable object. Set: models. SET (executable object)

Note: Django2 writes on_delete =models.CASCADE

2. One-to-one

One table becomes two tables, and the advantages are divided into tables according to the frequency of queries.

Field: OneToOneField

Parameters:

a,to

b,to_field

c,on_delete

3. Many-to-many

Field: ManyToManyField

Field parameters:to

V. Meta-information

Fields in metaclasses: Meta

index_together

Joint Index

unique_together

Joint Unique Index

28 July 2019, 10:42 | Views: 4032

Add new comment

For adding a comment, please log in
or create account

0 comments