14. The field models commonly used

 

Common Fields:

In Django, defined to be some Field mapping database table field type. The following describes those common field types.

Auto Field:

The database is mapped to an int, you can have characteristics of the automatic growth. Generally you do not need to use this type, if you do not specify a primary key, then the model will automatically generate a called id of the automatic growth of the primary key. If you want to specify a different name and with automatic growth of the primary key, use AutoField also possible.

BigAutoField:

Shaping 64, AutoField similar, only the range data is generated from the 1-9223372036854775807.

BooleanField:

Received at the model level is True / False. In the database level is tinyint type. If you do not specify a default value, the default value is None.

CharField:

In the database level is varchar type. In Python level is normal string. This type must specify a maximum length when in use, that must be passed max_length keyword parameters into account.

  1. navie time: do not know their time is indicated by which time zone. That is, I do not know a few two kilograms. More naive.
  2. Time aware: know your time is indicated by which time zone. That is more sober.

pytz library:

Library designed to handle time zones. This library is frequently updated data for some time zones, we do not need to worry about. And the library at the time of installation of Django will default installation. If not, you can by pip install pytzway of installation.

astimezone method:

Converting a time period of a time zone to another time zone. This method can only be awareinvoked type of time. It can not be navieinvoked type of time.
Sample code is as follows:

import pytz
from datetime import datetime
now = datetime.now() # 这是一个navie类型的时间
utc_timezone = pytz.timezone("UTC") # 定义UTC的时区对象
utc_now = now.astimezone(utc_timezone) # 将当前的时间转换为UTC时区的时间
>> ValueError: astimezone() cannot be applied to a naive datetime # 会抛出一个异常,原因就是因为navie类型的时间不能调用astimezone方法


now = now.replace(tzinfo=pytz.timezone('Asia/Shanghai'))
utc_now = now.astimezone(utc_timezone)
# 这时候就可以正确的转换。

replace method:

Some attributes can be a time of change.

django.utils.timezone.now method:

It will be based on settings.pywhether the set USE_TZ=Trueto get the current time. If set, then get a awaretype of UTCtime. If not set, you will get a navietype of time.

django.utils.timezone.localtime方法:

Based on setting.pythe TIME_ZONEto be a awaretype of time into TIME_ZONEthe time zone specified.

https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/

DateField:

Date type. In the Pythonmiddle is the datetime.datetype that can record date. It is mapped to the database datetype. Using this Fieldyou can pass the following parameters:
1 auto_now: this data every time you save time, use the current time. Such as a field recording a date of modification, this property can be set True.
2 auto_now_add: every time data is first added to it, they both use the current time. As an example, the first storage field recording, this property can be set True.

DateTimeField:

Datetime type, similar DateField. Not only can store dates, you can also store time. It is mapped to the database datetimetype. This Fieldcan also be used auto_nowand auto_now_addtwo properties.

TimeField:

Time type. Is the database timetype. In the Pythonmiddle is a datetime.timetype.

EmailField:

Similar CharField. It is also one of the underlying database varchartype. The maximum length is 254 characters.

FileField:

It used to store files. Please refer to the back of the file upload section section.

ImageField:

Used to store image files. Please refer to the back of the image upload section section.

FloatField:

Floating-point type. It is mapped to the database floattype.

IntegerField:

Shaping. Interval values are -2147483648——2147483647.

BigIntegerField:

Big plastic. Interval values are -9223372036854775808——9223372036854775807.

PositiveIntegerField:

Positive shaping. Interval values are 0——2147483647.

SmallIntegerField:

Small plastic. Interval values are -32768——32767.

PositiveSmallIntegerField:

Positive little shaping. Interval values are 0——32767.

TextField:

A large number of text types. Longtext is mapped to the database type.

UUIDField:

Only store uuidstring format. uuidIs a globally unique 32-bit strings, is generally used as the primary key.

URLField:

Similar to CharField, but can only be used to store the urlstring format. And the default max_lengthis 200.

Field commonly used parameters

null:

If set True, Djangowill specify the mapping table is empty at the time. The default is for False. In the string-related Fieldtime (CharField / TextField), the official recommended not to use this parameter, that is, keep the default value False. Because Djangothe process related to the string of Fieldtime, even if this Fieldis null=False, if you do not give this Fieldto pass any value, it Djangowill use an empty string ""as the default values are stored inside. Therefore, if re-use null=True, Djangoit generates two kinds of situations null (NULL or an empty string). If you want to allow this time form validation string is empty, it is recommended to use blank=True. If you FieldShi BooleanField, the corresponding empty fields can be compared NullBooleanField.

blank:

This field identifies the time form validation whether empty. Default False.
And that nullthere is a difference, nullis a pure database level. And blankthat level of form validation.

db_column:

The field name in the database. If you do not set this parameter, the model will use the name attribute.

default:

Defaults. It can be a value, or a function, but does not support lambdaexpressions. And does not support the variable list / dictionary / collection of data structures and the like.

primary_key:

Whether the primary key. Default False.

unique:

The value of the field in the table is unique. Generally set the phone number / email and so on.

More Fieldparameters, please refer to the official document: https://docs.djangoproject.com/zh-hans/2.0/ref/models/fields/

Guess you like

Origin www.cnblogs.com/ys-python/p/11266171.html