模型字段参考
字段选项
null
Field.null
如果为True,django会默认字段值为null,null默认为False。
注意空字符值会被存储为空字符而不是null。仅null=True 用于非字符类型字段,如整型,布尔,日期。如果你想允许form里空值还需要设置 blank=True, null 只和数据库存储有关。
blank
Field.blank
如果为True,字段值可以为空白,默认是False
choices
Field.choices
一个跌送的元组列表,用作一个选择字段。
如果提供值,django管理工具会用select来替代文本字段。
选择列表如下:
YEAR_IN_SCHOOL_CHOICES = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
)
元组的第一个元素会被存储,第二个是可读的选项名。
选择列表可如下同模型定义到一起
class Foo(models.Model):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
也可定义到模型外部
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
class Foo(models.Model):
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
还可以按组定义
MEDIA_CHOICES = (
('Audio', (
('vinyl', 'Vinyl'),
('cd', 'CD'),
)
),
('Video', (
('vhs', 'VHS Tape'),
('dvd', 'DVD'),
)
),
('unknown', 'Unknown'),
)
db_column
Field.db_column
数据库表列名,如果没有提供,默认使用字段名。
db_index
Field.db_index
如果为True,django-admin.py sqlindexes <sqlindexes> 将会创建索引。
db_tablespace
Field.db_tablespace
设置表的命名空间
default
Field.db_tablespace
设置字段默认值
editable
Field.editable
设置字段的可编辑性,默认为True
help_text
Field.help_text
设置字段的说明文字。
primary_key
Field.primary_key
如果为True,这个字段会被设置为主键。
unique
Field.unique
如果为True,这个字段值会被约束为唯一不重复。
unique_for_date
Field.unique_for_date
设置到一个DateField 或 DateTimeField 字段名,两个字段值会被约束唯一。
例如 title 字段设置has unique_for_date="pub_date",那么django就不允许有两条title和pub_date值同时相等的记录。
unique_for_month
Field.unique_for_month
如unique_for_date,只是约束月份
unique_for_year
Field.unique_for_year
如unique_for_date,只是约束年份
字段类型
AutoField
class AutoField(**options)
一个自动递增的整型字段,会被默认设置为primary key,如果你没有提供值,它会自动增加。
BooleanField
class BooleanField(**options)
布尔字段,管理工具里会自动将其描述为checkbox。
CharField
class CharField(max_length=None[, **options])
字符字段,CharField有一个必填参数,CharField. max_length,字符的最大长度,django会根据这个参数校验。
CommaSeparatedIntegerField
class CommaSeparatedIntegerField(max_length=None[, **options])
一个用逗号分割整数的字段,和charfield基本属性类似。
DateField
class DateField([auto_now=False, auto_now_add=False, **options])
日期字段 有几个参数
DateField. auto_now 自动更新为当前时间,每次对模型中的字段修改,这个字段都会自动更新问当前世嘉相当于lastmodify时间印章
DateField. auto_now_add 模型保存的时候,如果为指定指端值,会默认为当前时间。
DateTimeField
class DateTimeField([auto_now=False, auto_now_add=False, **options])
日期时间字段,参数类似 DateField
DecimalField
New in Django 1.0.
<!---->class DecimalField (max_digits=None , decimal_places=None [ , **options ] )
小数字段,需要两个参数:DecimalField. max_digits 最大位数
<!---->DecimalField. decimal_places 小数位数
EmailField
<!---->class EmailField ([ max_length=75 , **options ] )
电子邮件字段,会校验电子邮件输入格式的有效性。
FileField
<!---->class FileField (upload_to=None [ , max_length=100 , **options ] )
文件上传字段,有个必填字段FileField. upload_to 文件的存储路径
FileField. storage New in Django 1.0 文件临时保存路径
FilePathField
class FilePathField(path=None[, match=None, recursive=False, max_length=100, **options])
选择指定目录按限制规则选择文件,有几个参数:
FilePathField.path 指定绝对路径
FilePathField.match 匹配规则
FilePathField.recursive 是否包含子目录
FilePathField(path="/home/images", match="foo.*", recursive=True)
FloatField
class FloatField([**options])
浮点型字段
ImageField
class ImageField(upload_to=None[, height_field=None, width_field=None, max_length=100, **options])
图像字段
ImageField.height_field 通过模型的其他字段指定图像高。
ImageField.width_field 通过模型的其他字段指定图宽。
IntegerField
class IntegerField([**options])
整型字段
IPAddressField
class IPAddressField([**options])
IP类型字段
NullBooleanField
class NullBooleanField([**options])
类似BooleanField,但是允许为null。
PositiveIntegerField
class PositiveIntegerField([**options])
正整数字段
PositiveSmallIntegerField
class PositiveSmallIntegerField([**options])
正小整型字段
SlugField
class SlugField([max_length=50, **options])
是一个报纸术语. slug 是某个东西的小小标记(短签), 只包含字母,数字,下划线和连字符.它们通常用于URLs
SmallIntegerField
class SmallIntegerField([**options])
小整型字段
TextField
class TextField([**options])
文本字段,管理工具会描述为textarea
TimeField
class TimeField([auto_now=False, auto_now_add=False, **options])
时间字段
URLField
class URLField([verify_exists=True, max_length=200, **options])
URL 字段
URLField.verify_exists 验证地址是否有效
XMLField
class XMLField(schema_path=None[, **options])
XML字符字段
schema_path 用来校验文本的 RelaxNG_ schema 的文件系统路径
ForeignKey
class ForeignKey(othermodel[, **options])
多对一关系字段,需要定位到一个模型,如果是自身递归关系models.ForeignKey('self').
class Car(models.Model):
manufacturer = models.ForeignKey('Manufacturer')
# ...
class Manufacturer(models.Model):
# ...
ForeignKey.limit_choices_to 限制关联的条件
limit_choices_to = {'pub_date__lte': datetime.now}
ForeignKey.related_name 关联字段名
ForeignKey.to_field 关联目标字段
ManyToManyField
<!---->class ManyToManyField(othermodel[, **options])
多对多字段
ManyToManyField.related_name 关联字段名
ManyToManyFields.limit_choices_to 限制关联的条件
ManyToManyFields.symmetrical 用于自己对自己的多对多
ManyToManyFields.through 使用中间模型
ManyToManyField.db_table 多对多关系表
OneToOneField
<!---->class OneToOneField(othermodel[, parent_link=False, **options])
一对一字段
OneToOneField.parent_link 父级链接
分享到:
相关推荐
Django 1.0 中文文档.docDjango 1.0 中文文档.docDjango 1.0 中文文档.docDjango 1.0 中文文档.doc
django 1.0官方的使用文档chm打包
django-python3-ldap, python 3的Django LDAP用户身份验证后端 django-python3-ldapdjango-python3-ldap 为 python 2和 3提供了一个 Django LDAP用户身份验证后端。特性使用LDAP服务器验证用户身份。将LDAP用户与...
资源来自pypi官网。 资源全名:django-tables2-column-shifter-0.3.0.tar.gz
python库,解压后可用。 资源全名:django_admin_multilang-1.0-py3-none-any.whl
Django1.0以上版本的教材-DjangoBook2.0中文版.pdf 适用于Django1.0以上的环境
资源分类:Python库 所属语言:Python 资源全名:djangocms_algolia-1.3.1.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
为支持django3对django_cas_ng进行修改 **Features** - Support CAS version 1.0, 2.0, 3.0 - Support Single Sign Out - Configuration of services via the django Admin application - Fine control on ...
Python在线考试系统后端-大学毕业设计-基于Django+Django -Rest-Framework Python在线考试系统后端-大学毕业设计-基于Django+Django -Rest-Framework Python在线考试系统后端-大学毕业设计-基于Django+Django -Rest-...
django-admin-bootstrap-master.zip
Django1.0速查手册,Django 1.0 Cheat sheet
1、基于Django+Django -Rest-Framework+Python的在线考试系统后端(源码).zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目...
django-classy-tags, 基于类的Django 模板标记 django-classy-tags请参考文档/目录中的文档以获得帮助。 有关HTML呈现版本的信息,请参见这里的 。 这里项目这个项目的目标是创建一种新的编写 Django 模板标签的方法...
python库。 资源全名:django-microsip-base-1.0.5.zip
python库。资源全名:django-select2-forms-3.0.0.tar.gz
python库。 资源全名:django-taggit-serializer-0.1.5.tar.gz
资源来自pypi官网。 资源全名:django_pumaska-1.0rc2-py3-none-any.whl