字段 - 类型
-
models.AutoField() 自增列=int(11),如果没有的话,默认会生成一个名称为id的列,如果要显示的定义一个自增列,必须把该列设置为主键(primary_key=True)
-
models.IntegerField() 整形(-2147483648, 2147483647)
-
models.BigIntegerField() 长整形(-9223372036854775808, 9223372036854775807)
-
models.PositiveIntegerField() 正Integer (0, 2147483647)
-
models.PositiveSmallIntegerField()正smallInteger (0, 32767)
-
models.SmallIntegerField() 短整型 (-32768, 32767)
-
models.Decimal() 十进制小数类型=decimal 必须指定max_digits(总位数)和decimal_places(小数位)
-
models.FloatField() 浮点类型=double
-
models.CharField() 字符串类型字段 必须加max_length参数
- models.ComaSeparatedIntegerField()用逗号分割的数字类型=varchar 继承CharField,所以必须加max_lenght参数
- models.EmailField() 字符串类型(正则表达式邮箱)=varchar 对字符串进行正则表达式验证
- models.IPAddressField() 字符串类型(ip4正则表达式)
- models.GenericIPAddressField() 字符串类型(ip4和ip6是可选的) 参数protocol可以是:both、ipv4、ipv6 验证时,会根据设置进行报错
- models.SlugField() 减号、下划线、字母、数字
- models.URLField() 字符串类型,地址正则表达式
- models.BinaryField() 二进制
- models.ImageField() 图片 upload_to=’’ 指定上传图片的路径
- models.FilePathField() 文件 upload_to=’’ 指定上传图片的路径
-
models.TextField 字符串=longtext
-
models.BooleanField() 布尔类型字段=tinyint(1) 不能为空blank=True
-
models.NullBooleanField() 允许为空的布尔类型
-
models.DateField() 日期字段类型date 参数auto_now=True表示每次更新都会更新这个时间;参数auto_now_add表示只是第一次创建时添加,之后的更新不再改变
-
models.DateTimeField() 日期字段类型datetime 同DateField的参数
-
models.TimeField() 时间 HH:MM[:ss[.uuuuuu]]
字段 - 选项:
-
null=布尔值: 与数据库相关,字段是否允许为空,默认False
-
blank=布尔值: 与表单验证相关,表单是否允许为空,默认False
-
defalut=值或对象: 字段默认值,
-
primary_key=布尔值: 是否主键,默认False
-
unique=布尔值: 是否唯一,默认False
-
verbose_name=文本: 字段描述/form中的label值,也可以放到第一个参数models.CharField(“person’s first name”, max_length=30)
-
db_column 数据库中字段的列名(db_column=“test”)
-
db_index 设置索引,默认False
-
editable 是否可编辑,默认为True
-
help_text 表单帮助信息
-
choices form中显示选择框的内容,用不变动的数据放在内存中从而避免跨表操作, 可通过get_字段_display() 访问字段的显示值
gf = models.IntegerField(choices=[(0, '何穗'),(1, '大表姐'),],default=1)
-
error_messages 自定义错误信息(字典类型),字典健:null, blank, invalid, invalid_choice, unique, and unique_for_date 如:
{'null': "不能为空.", 'invalid': '格式错误'}
-
validators 自定义错误验证(列表类型),从而定制想要的验证规则
from django.core.validators import RegexValidator
from django.core.validators import EmailValidator,URLValidator,DecimalValidator,\
MaxLengthValidator,MinLengthValidator,MaxValueValidator,MinValueValidator
test = models.CharField(
max_length=32,
error_messages={
'c1': '优先错信息1',
'c2': '优先错信息2',
'c3': '优先错信息3',
},
validators=[
RegexValidator(regex='root_\d+', message='错误了', code='c1'),
RegexValidator(regex='root_112233\d+', message='又错误了', code='c2'),
EmailValidator(message='又错误了', code='c3'), ]
)
-
max_length: [字符串型] 必须指定数值的参数,utf8编码的最大长度
-
unique_for_date: [时间日期类型] 日期必须唯一
-
unique_for_month: [时间日期类型] 月必须唯一
-
unique_for_year: [时间日期类型] 年必须唯一
-
auto_now: [时间日期类型] 更新当前时间
-
auto_now_add: [时间日期类型] 创建时更新当前时间
-
max_digits: [浮点型] DecimalField字段必须指定,一共多少位
-
decimal_places: [浮点型] DecimalField字段必须指定,小数点位数
blank主要用在CharField, TextField 这两个字符型字段可用字符串来存储空值
null主要用在IntegerField,DateField,DateTimeField 这几个字段不接受空字符串,所以在使用时,必须将blank和null同时赋值True
元数据Meta
class Meta:
db_table = "addressinfo" # 表的名称
ordering = 'pid' # 排序字段
verbose_name = "省市县地址信息"
verbose_name_plural = verbose_name # 复数
unique_together = ("address", "note") # 联合唯一健,还可以用二维元组 ((), ())
app_label = 'app_name' # 模型类属于哪一个应用
# db_tablespace # 定义数据库表空间的名称
# abstract = True # 基类用于继承,默认为False
表关系
示例:
from django.db import models
class School(models.Model):
''' 学院 '''
id = models.AutoField(primary_key=True)
name = models