第三章 常用models的Field
1、models.AutoField
自增列 = int(11)
如果不写,默认会生成一个名称为 id 的列
如果要显式的自定义一个自增列,必须设置primary_key=True。
models.AutoField(primary_key=True)
2、models.CharField
字符串字段
必须设置参数,即字符串长度
models.CharField(max_length=50)
3、models.BooleanField
布尔类型=tinyint(1)
不能为空,可添加Blank=True
models.BooleanField(Blank=True)
4、models.ComaSeparatedIntegerField
用逗号分割的数字=varchar
继承CharField,所以必须 max_lenght 参数
models.ComaSeparatedIntegerField(max_lenght =50)
5、models.DateField
日期类型 date
DateField.auto_now:保存时自动设置该字段为现在日期,最后修改日期
DateField.auto_now_add:当该对象第一次被创建是自动设置该字段为现在日期,创建日期。
6、models.DateTimeField
日期时间类型 datetime
同DateField的参数
7、models.Decimal
十进制小数类型 = decimal
DecimalField