1、AutoField
一个自增的IntegerField,一般不直接使用,Django会自动给每张表添加一个自增的primary key。
2、BigIntegerField
64位整数, -9223372036854775808 到 9223372036854775807。默认的显示widget 是 TextInput.

3、BinaryField ( Django 1.6 版本新增 )
存储二进制数据。不能使用 filter 函数获得 QuerySet

4、BooleanField
True/False,默认的widget 是 CheckboxInput。
如果需要置空,则必须用 NullBooleanField 代替。
Django 1.6 修改:BooleanField 的默认值 由 False 改为 None,在 default 属性未设置的情况下。

5、CharField
存储字符串。必须有 max_length 参数指定长度。默认的form widget 是 TextInput
如果字符串巨长,推荐使用 TextField。

6、CommaSeparatedIntegerField
一串由逗号分开的整数。必须有 max_length 参数。

7、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 设置。

8、DateTimeField
日期+时间。与python里的 datetime.datetime 实例同。常用附加选项和DateField一样。
默认 form widget 是一个 TextInput

9、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.

10、EmailField
在 CharField 基础上附加了 邮件地址合法性验证。不需要强制设定 max_length
注意:当前默认设置 max_length 是 75,虽然已经不符合标准,但未了向前兼容,未修改。
11、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 来改变这个大小。
11、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实例 被删除之后,相关联的文件并没有被删除,需要自己清除!

12、FloatField
与 python 里的 float 实例相同,默认的 form widget 是 TextInput。
虽然 FloatField 与 DecimalField 都是表示实数,但却是不同的表现形式,FloatField 用的是 python d float 类型,但是 DecimalField 用的却是 Decimal 类型。区别可见:http://docs.python.org/2.7/library/decimal.html#decimal
13、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 改大小。
14、IntegerField
整数,默认的form widget 是 TextInput。
15、IPAddressField
IP地址,字符串类型,如 127.0.0.1。默认 form widget 是 TextInput。
16、TextField
大文本,巨长的文本。默认的 form widget 是 Textarea。
注意,如果使用 MySQLdb 1.2.1p2 和 utf-8_bin 编码,会有一些问题https://docs.djangoproject.com/en/dev/ref/databases/#mysql-collation。具体问题未分析,可自行避开。
17、URLField
加了 URL 合法性验证的 CharField。
默认的 form widget 是 TextInput。
默认max_length=200,可修改。
18、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 异常。

http://blog.csdn.net/lianghx001/article/details/24350567

django【ORM】model字段类型的更多相关文章

  1. Django model字段类型清单

    转载:<Django model字段类型清单> Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField ...

  2. Django model 字段类型清单

    Django model字段类型清单 Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField 一个自动递增的整型字 ...

  3. Django ORM 常用字段和参数

    Django ORM 常用字段和参数 一:常用字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. I ...

  4. Django(ORM常用字段)

    day68 参考:http://www.cnblogs.com/liwenzhou/p/8688919.html 1. Django ORM常用字段:             1. AutoField ...

  5. Django框架之第六篇(模型层)--单表查询和必知必会13条、单表查询之双下划线、Django ORM常用字段和参数、关系字段

    单表查询 补充一个知识点:在models.py建表是 create_time = models.DateField() 关键字参数: 1.auto_now:每次操作数据,都会自动刷新当前操作的时间 2 ...

  6. Django orm常用字段和字段参数

    1.Object Relational Mapping(ORM) 1.1ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象 ...

  7. django的model字段在保存的时候做预处理怎么办?

    django的model字段在保存的时候做预处理怎么办? 比如这个model: class Book(Model): publish_date = DateField() 但是在保存时,用户输入数据是 ...

  8. Django model 字段类型及选项解析

    字段类型选择: AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 ...

  9. Django——model字段类型 2

    Django 通过models实现数据库的创建.修改.删除等操作,Django中model作为数据资源指定了字段以及一些相应的功能,通常每个model对应数据库中的一张表,(每个model都是从dja ...

随机推荐

  1. luogu 1360 阵容均衡(前缀和+差分+hash)

    要求一段最大的区间里每个能力的增长值是一样的. 我们首先求一遍前缀和,发现,如果区间内[l,r]每个能力的增长值是一样的话,那么前缀和[r]和[l-1]的差分也应该是一样的. 那么我们把前缀和的差分h ...

  2. Authenticator及AuthenticationStrategy

    Authenticator的职责是验证用户帐号,是Shiro API中身份验证核心的入口点: 如果验证成功,将返回AuthenticationInfo 验证信息:此信息中包含了身份及凭证:如果验证失败 ...

  3. MySQL5.7初始配置

    MySQL5.7初始配置 Windows7 环境安装MySQL5.7配置命令 <<<<<<<<<<<<<<<& ...

  4. 【刷题】BZOJ 4650 [Noi2016]优秀的拆分

    Description 如果一个字符串可以被拆分为 AABBAABB 的形式,其中 AA 和 BB 是任意非空字符串,则我们称该字符串的这种拆分是优秀的.例如,对于字符串 aabaabaa,如果令 A ...

  5. 【刷题】清橙 A1295 necklace

    试题来源 清华大学2011年百名信息学优秀高中学子夏令营 问题描述 有人打算送给你一条宝石项链,包含了N颗五颜六色(一共有M种颜色)的宝石.因为本问题中你只关心每个宝石的颜色,而且项链现在两头还没有接 ...

  6. 【JQuery】事件

    一.前言        接着上一章选择器的知识,继续啊jQuery的学习 二.内容 $(function(){}) 文档初始化加载 event.pageX 相对于文档左边缘的鼠标位置 event.pa ...

  7. BZOJ3524 & LOJ2432:[POI2014]代理商Couriers——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3524 https://loj.ac/problem/2432 给一个长度为n的序列a.1≤a[i] ...

  8. 框架----Django之Form组件

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 一.小试牛刀 1.创建Form类 from d ...

  9. 【神仙题】【CF28D】 Don't fear, DravDe is kind

    传送门 Description 一个有N辆卡车的车队从城市Z驶向城市3,来到了一条叫做"恐惧隧道"的隧道.在卡车司机中,有传言说怪物DravDe在那条隧道里搜寻司机.有些司机害怕先 ...

  10. count distinct

    SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders