当前位置:首页 > python-django 模型model字段类型说明

python-django 模型model字段类型说明

点击次数:4577  更新日期:2011-10-22

V=models.CharField(max_length=None<, **options>)    #varchar
V=models.EmailField()    #varchar
V=models.URLField()    #varchar
V=models.FileField(upload_to=None<, max_length=100, **options>)    #varchar
#upload_to指定保存目录可带格式,
V=models.ImageField(upload_to=None<, height_field=None, width_field=None, max_length=100, **options>)
V=models.IPAddressField(<**options>)    #varchar
V=models.FilePathField(path=None<, match=None, recursive=False, max_length=100, **options>) #varchar
V=models.SlugField()    #varchar,标签,内含索引
V=models.CommaSeparatedIntegerField(max_length=None<, **options>)    #varchar
V=models.IntegerField(<**options>)    #int
V=models.PositiveIntegerField(<**options>)    #int 正整数
V=models.SmallIntegerField(<**options>)    #smallint
V=models.PositiveSmallIntegerField(<**options>)    #smallint 正整数
V=models.AutoField(**options)    #int;在Django代码内是自增
V=models.DecimalField(max_digits=None, decimal_places=None<, **options>)    #decimal
V=models.FloatField(<**options>)    #real
V=models.BooleanField(**options)    #boolean或bit
V=models.NullBooleanField(<**options>)    #bit上可以设置上null值
V=models.DateField()    #date
#auto_now最后修改记录的日期;auto_now_add添加记录的日期
V=models.DateTimeField()    #datetime
V=models.TimeField()    #time
V=models.TextField(<**options>)    #text
V=models.XMLField(schema_path=None<, **options>)    #text
——————————————————————————–
V=models.ForeignKey(othermodel<, **options>)    #外键,关联其它模型,创建关联索引
V=models.ManyToManyField(othermodel<, **options>)    #多对多,关联其它模型,创建关联表
V=models.OneToOneField(othermodel<, parent_link=False, **options>)    #一对一,关联表属性

\r\r\n

 

\r\r\n


django 中model 作为数据资源指定了以及一些处理该相应的功能。通常情况下,每个model对应数据库中的一张表
每个model都是从django.core.meta.Model中派生出来的model中每个属性(class attribute)对应着数据库表中的一个信息(Model metadata)在类中被称作METAField Objects每个class attributes对应着数据库中的一个,其应该是由meta.Filed的派生的。
from django.core import metaclass Person(meta.Model):    first_name = meta.CharField(maxlength=30)    last_name = meta.CharFiled("lastname",maxlength=30)
除了 ForeignKey, ManytoManyFiled OneToOneField 使用verbose_name关键以外,第一个可选参数是用来为命名的,如果不设置,系统会自动为起起名。sites = meta.ManyToManyField(site,verbose_name="list of sites")
Field 选项
null     null 缺省设置为false.通常不将其用于字符型上,比如CharField,TextField上.字符型如果没有值会返回空字符串。
blank    该是否可以为空。如果为假,则必须有值
choices    一个用来选择值的2维元组。第一个值是实际存储的值,第二个用来方便进行选择。    SEX_CHOICES= (('F','Female'),('M','Male'),)
coredb_columndb_index    如果为真将为此创建索引default    缺省值
editable    如果为假,admin模式下将不能改写。缺省为真
help_text    admin模式下帮助文档
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    数据唯一
unique_for_date    title =  meta.CharField(maxlength=30,unique_for_date='pub_date')    系统将不允许title和pub_date两个都相同的数据重复出现
unique_for_month / unique_for_year
validator_list    有效性检查。非有效产生 django.core.validators.ValidationError 错误
Field Types
AutoField     自动增长的整型。通常无需自己添加
BooleanFieldCharField    对于大量文字应该使用TextField。参数: maxlength
DateField    参数: auto_now  自动设置为对象保存时刻的时间。对于类似最后改动之类的应用很有效    auto_now_add  从最初保存对象至今的增量时间
DateTimeField 同上EmailField     检查是否为email的CharField
FileField    保存文件的相对路径名称    绝对路径需要在setting中设置 MEDIA_ROOT。对于ImageField设置 MEDIA_URL。    参数upload_to.相对于MEDIA_ROOT目录的子目录    通过get_<fieldname>_url获取绝对路径
FilePathField    通过给定参数获取文件列表(个人感觉)    path 必须要有。 文件系统的绝对路径    match 可选。一个正则表达式。用来做文件筛选过滤    recursive 可选。True 包含子目录查找    FilePathField(path="/home/images", match="foo.*" , recursive=True)
FloatField    浮点数字。两个必须选项    max_digits 数字最多位数    decimal_places 小数点位置    meta.FloatField(...,max_digits=5,decimal_places=2)   /////100.00
ImageField    基本上同FileField.会进行文件格式检查。两个额外选项    height_field, width_field 如果设置这两项,会自动对图片进行缩放后再保存    需要 python Imaging library
IntegerFieldIPAddressField    "10.1.1.3"
NullBooleanField    比BooleanField多了一个NULL状态
PhoneNumberField    美国格式电话号码 xxx-xxx-xxxx. CharField
PositiveIntegerField    正整数
PositiveSmallIntegerField    一个小的正整数。根据数据库的设置
SlugField    小片断。什么都可以。通常用来存url    SlugField 被暗指 maxlength=50 db_index=True    选项: prepopulate_from 一个用来自动生成slug的列表
SmallIntegerFieldTextField
TimeField    选项: auto-population(可能不对) from DateField and DateTimeField
URLField    选项: verify_exists  如果为True ,系统自动检查该url是否存在不为404
USStateField    两字符美国州简写
XMLField    保存xml数据。    选项:schema_path .指定relaxNG格式schema用来检验xml合法性