当前位置: 移动技术网 > IT编程>脚本编程>Python > django中model字段与属性

django中model字段与属性

2018年11月12日  | 移动技术网IT编程  | 我要评论

冰崩,上海海关学院招生网,棒球小子牛仔裤

model field 类型
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 异常。
如何实现检查器 见:https://docs.djangoproject.com/en/dev/ref/validators/

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网