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.
navie time and time aware:
What is navie time? What is aware of time?
- 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.
- 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 pytz
way of installation.
astimezone method:
Converting a time period of a time zone to another time zone. This method can only be aware
invoked type of time. It can not be navie
invoked 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.py
whether the set USE_TZ=True
to get the current time. If set, then get a aware
type of UTC
time. If not set, you will get a navie
type of time.
django.utils.timezone.localtime方法:
Based on setting.py
the TIME_ZONE
to be a aware
type of time into TIME_ZONE
the time zone specified.
navie and aware and in django introduction of usage:
https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/
DateField:
Date type. In the Python
middle is the datetime.date
type that can record date. It is mapped to the database date
type. Using this Field
you 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 datetime
type. This Field
can also be used auto_now
and auto_now_add
two properties.
TimeField:
Time type. Is the database time
type. In the Python
middle is a datetime.time
type.
EmailField:
Similar CharField
. It is also one of the underlying database varchar
type. 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 float
type.
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 uuid
string format. uuid
Is 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 url
string format. And the default max_length
is 200.
Field commonly used parameters
null:
If set True
, Django
will specify the mapping table is empty at the time. The default is for False
. In the string-related Field
time (CharField / TextField), the official recommended not to use this parameter, that is, keep the default value False
. Because Django
the process related to the string of Field
time, even if this Field
is null=False
, if you do not give this Field
to pass any value, it Django
will use an empty string ""
as the default values are stored inside. Therefore, if re-use null=True
, Django
it 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 Field
Shi BooleanField
, the corresponding empty fields can be compared NullBooleanField
.
blank:
This field identifies the time form validation whether empty. Default False
.
And that null
there is a difference, null
is a pure database level. And blank
that 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 lambda
expressions. 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 Field
parameters, please refer to the official document: https://docs.djangoproject.com/zh-hans/2.0/ref/models/fields/