django models 类型整理 version:1.8.3
网上百度到的最上面的一篇已经是11年的了,django变化很大,现在把1.8.3版的models类型大致整理了下贴出来
普通键部分
Field | Params | in database | attention |
---|---|---|---|
AutoField | **options | integer | 在django代码内是自增 |
BooleanField | **options | boolean\bit | |
BinaryField | [**options] | blob | |
BigIntegerField | [**options] | integer | 继承自Integer |
CharField | max_length=None[, **options] | varchar | |
CommaSeparatedIntegerField | max_length=None[, **options] | 继承自charField | |
DateField | [auto_now=False, auto_now_add=False, **options] | date | auto_now最后修改记录的日期;auto_now_add添加记录的日期 |
DateTimeField | [auto_now=False, auto_now_add=False, **options] | datetime | auto_now最后修改记录的日期;auto_now_add添加记录的日期,继承自DateField |
DecimalField | max_digits=None, decimal_places=None[, **options] | decimal | |
DurationField | default=[,**options] | 存储时间段,但是貌似django的开发团队还没有完善这个,有一些bug | |
EmailField | [max_length=75, **options] | varchar | |
FileField | update_to = None[, max_length=100, **options] | varchar | update_to 指定保存目录可带格式 |
FilePathField | path=None[, match=None, recursive=False, max_length=100, **options] | varchar | |
FloatField | [**options] | real | |
GenericIPAddressField | [**options, unpack_ipv4=False, protocol=u’both’] | ||
ImageField | upload_to=None[, height_field=None, width_field=None, max_length=100, **options] | varchar | |
IntegerField | [**options] | integer | |
IPAddressField | [**options] | varchar | |
ManyToManyField | othermodel[, **options] | ||
NullBooleanField | [**options] | bit字段上可以设置null值 | |
PositiveIntegerField | [**options] | integer | 继承自IntegerField |
PositiveSmallIntegerField | [**options] | smallinteger | |
URLField | [verify_exists=True, max_length=200, **options] | varchar | |
UUIDField | [verbose_name=None, **options] | ||
SlugField | [max_length=50, **options] | varchar | 标签,内含索引 |
SmallIntegerField | [**options] | smallint | 继承自IntegerField |
TimeField | [auto_now=False, auto_now_add=False, **options] | time | auto_now最后修改记录的日期;auto_now_add添加记录的日期 |
TextField | [**options] | text | |
XMLField | schema_path=None[, **options] | text |
外键部分:
Field | Params | attention |
---|---|---|
ForeignKey | othermodel[, **options] | 直接用model当做外键,默认是多对一的关系 |
ManyToManyField | othermodel[, **options] | 多对多,关联其它模型,创建关联表 |
OneToOneField | othermodel[, parent_link=False, **options] | 一对一,字段关联表属性 |
附options的部分:
- verbose_name=None
- name=None
- primary_key=False
- max_length=None
- unique=False
- blank=False
- null=False
- db_index=False
- rel=None
- default=
<class django.db.models.fields.NOT_PROVIDED>
- editable=False
- serialize=True
- unique_for_date=None
- unique_for_month=None
- unique_for_year=None
- choices=None
- help_text=u’’
- db_column=None
- db_tablespace=None
- auto_created=False
- validators=[]
- error_messages=None