model field 类型
一、AutoField
一个自增的IntegerField,通常不直接使用,Django会自动给每张表添加一个自增的primary key。
二、BigIntegerField
64位整数, -9223372036854775808 到 9223372036854775807。默认的显示widget 是 TextInput.
三、BinaryField ( Django 1.6 版本新增 )
存储二进制数据。不能使用 filter 函数得到 QuerySet
四、BooleanField
True/False,默认的widget 是 CheckboxInput。
若是须要置空,则必须用 NullBooleanField 代替。
Django 1.6 修改:BooleanField 的默认值 由 False 改成 None,在 default 属性未设置的状况下。
五、CharField
存储字符串。必须有 max_length 参数指定长度。默认的form widget 是 TextInput
若是字符串巨长,推荐使用 TextField。
六、CommaSeparatedIntegerField
一串由逗号分开的整数。必须有 max_length 参数。
七、DateField
日期,与python里的datetime.date 实例同。有如下几个可选的选项,均为bool类型:
DateField.auto_now: 每次执行 save 操做的时候自动记录当前时间,常做为最近一次修改的时间 使用。注意:老是在执行save 操做的时候执行,没法覆盖。
DateField.auto_now_add: 第一次建立的时候添加当前时间。常做为 建立时间 使用。注意:每次create 都会调用。
默认的form widget 是 TextInput。
注意:设置auto_now 或者 auto_now_add 为 True 会致使当前自动拥有 editable=False 和 blank = True 设置。
八、DateTimeField
日期+时间。与python里的 datetime.datetime 实例同。经常使用附加选项和DateField同样。
默认 form widget 是一个 TextInput
九、DecimalField
设置了精度的十进制数字。
A fixed-precision decimal number, represented in Python by a Decimal instance. Has two required arguments:
DecimalField.max_digits
The maximum number of digits allowed in the number. Note that this number must be greater than or equal to decimal_places.
DecimalField.decimal_places?
The number of decimal places to store with the number.
For example, to store numbers up to 999 with a resolution of 2 decimal places, you’d use:
models.DecimalField(..., max_digits=5, decimal_places=2)
And to store numbers up to approximately one billion with a resolution of 10 decimal places:
models.DecimalField(..., max_digits=19, decimal_places=10)
The default form widget for this field is a TextInput.
十、EmailField
在 CharField 基础上附加了 邮件地址合法性验证。不须要强制设定 max_length
注意:当前默认设置 max_length 是 75,虽然已经不符合标准,但未了向前兼容,未修改。
十一、FileField
文件上传。不支持 primary_key 和 unique 选项。不然会报 TypeError 异常。
必须设置 FileField.upload_to 选项,这个是 本地文件系统路径,附加在 MEDIA_ROOT 设置的后边,也就是 MEDIA_ROOT 下的子目录相对路径。
默认的form widget 是 FileInput。
使用 FileField 和 ImageField 须要如下步骤:
(1)修改 settting.py,设置 MEDIA_ROOT(使用绝对路径),指定用户上传的文件保存在哪里。设置 MEDIA_URL,做为 web地址 前缀,要保证 MEDIA_ROOT 目录对运行 Django 的用户是可写的;
(2)在 model 中增长 FileField 或 ImageField,并指定 upload_to 选项指定存在 MEDIA_ROOT 的哪一个子目录里;
(3)存在数据库里的是什么东西呢?是 File 或 Image相对于 MEDIA_ROOT 的相对路径,你能够在 Django 里方便的使用这个地址,好比你的 ImageField 叫 tupian,你能够在 template 中用{{object.tupian.url}}。
举个例子:假设你的 MEDIA_ROOT='/home/media',upload_to 设置为 'photos/%Y/%m/%d','%Y/%m/%d' 部分使用strftime() 提供。若是你在 2013年10月10日上传了一个文件,那么它就存在 /home/media/photos/2013/10/10/ 下。
文件在 model实例 执行 save操做的同时保存,因此文件在model实例执行save以前,硬盘的上的文件名的是不可靠的。
注意:要验证用户上传的文件确实是本身须要的,以防止安全漏洞出现。
默认状况下,FileField 在数据库中表现为 varchar(100) 的一个列。你可使用 max_length 来改变这个大小。
十一、FileField 和 FieldFile
当你访问 一个 model 内的 FileField 时,将获得一个 FieldFile 实例来访问实际的文件。这个类提供了几个属性和方法用来和实际的文件数据交互:
FieldFile.url:只读属性,获取文件的相对URL地址;
FieldFile.open( mode = 'rb' ):打开文件,和python 的 open 同样;
FieldFile.close():和 python 的 file.close() 同样;
FieldFile.save( name, content, save=True ):name 是文件名,content 是包含了文件内容的 django.core.files.File 实例,与 python 的 file 不同。The optional save argument controls whether or not the instance is saved after the file has been altered. Defaults to True。
两种方式 进行 content 设置:
from django.core.files import File
f = open( 'helo.txt' )
content = File(f)
另外一种是:
from django.core.files.base import ContentFile
content = ContentFile( 'helloworld' )
更多内容可见:https://docs.djangoproject.com/en/dev/topics/files/
FieldFile.delete( save = True ):删除当前的文件。若是文件已经打开,则自动关闭。The optional save argument controls whether or not the instance is saved after the file has been deleted. Defaults to True.
值得注意的是:当一个 model实例 被删除以后,相关联的文件并无被删除,须要本身清除!
十二、FloatField
与 python 里的 float 实例相同,默认的 form widget 是 TextInput。
虽然 FloatField 与 DecimalField 都是表示实数,但倒是不一样的表现形式,FloatField 用的是 python d float 类型,可是 DecimalField 用的倒是 Decimal 类型。区别可见:http://docs.python.org/2.7/library/decimal.html#decimal
1三、ImageField
在 FileField 基础上加上是不是合法图片验证功能的一个类型。
除了 FileField 有的属性外,ImageField 另有 height 和 width 属性。
To facilitate querying on those attributes, ImageField has two extra optional arguments:
ImageField.height_field
Name of a model field which will be auto-populated with the height of the image each time the model instance is saved.
ImageField.width_field
Name of a model field which will be auto-populated with the width of the image each time the model instance is saved.
注意:须要安装 PIL 或者 Pillow 模块。在数据库中一样表现为 varchar(100),可经过 max_length 改大小。
1四、IntegerField
整数,默认的form widget 是 TextInput。
1五、IPAddressField
IP地址,字符串类型,如 127.0.0.1。默认 form widget 是 TextInput。
1六、TextField
大文本,巨长的文本。默认的 form widget 是 Textarea。
注意,若是使用 MySQLdb 1.2.1p2 和 utf-8_bin 编码,会有一些问题https://docs.djangoproject.com/en/dev/ref/databases/#mysql-collation。具体问题未分析,可自行避开。
1七、URLField
加了 URL 合法性验证的 CharField。
默认的 form widget 是 TextInput。
默认max_length=200,可修改。
1八、ForeignKey / ManyToManyField / OneToOneField / SmallIntegerField / SlugField / PositiveSmallIntegerField / PositiveIntegerField
Field 选项
null
boolean 值,缺省设置为false。一般不将其用于字符型字段上,好比CharField,TextField上。字符型字段若是没有值会返回空字符串。
blank
boolean 值,该字段是否能够为空。若是为假,则必须有值。
choices
元组值,一个用来选择值的2维元组。第一个值是实际存储的值,第二个用来方便进行选择。如SEX_CHOICES=((‘F’,’Female’),(‘M’,’Male’),)
db_column
string 值,指定当前列在数据库中的名字,不设置,将自动采用model字段名;
db_index
boolean 值,若是为True将为此字段建立索引;
default
给当前字段设定的缺省值,能够是一个具体值,也能够是一个可调用的对象,若是是可调用的对象将每次产生一个新的对象;
editable
boolean 值,若是为假,admin模式下将不能改写。缺省为真;
error_messages
字典,设置默认的出错信息,可覆盖的key 有 null, blank, invalid, invalid_choice, 和 unique。
help_text
admin模式下帮助文档
form widget 内显示帮助文本。
primary_key
设置主键,若是没有设置django建立表时会自动加上:id = meta.AutoField(‘ID’, primary_key=True)
primary_key=True implies blank=False, null=False and unique=True. Only one primary key is allowed on an object.
radio_admin
用于 admin 模式下将 select 转换为 radio 显示。只用于 ForeignKey 或者设置了choices
unique
boolean值,数据是否进行惟一性验证;
unique_for_date
字符串类型,值指向一个DateTimeField 或者 一个 DateField的列名称。日期惟一,以下例中系统将不容许title和pub_date两个都相同的数据重复出现
title = meta.CharField( maxlength=30, unique_for_date=’pub_date’ )
unique_for_month / unique_for_year
用法同上
verbose_name
string类型。更人性化的列名。
validators
有效性检查。无效则抛出 django.core.validators.ValidationError 异常。
如何实现检查器 见:https://docs.djangoproject.com/en/dev/ref/validators/html