Django database fields and parameters

Field parameters:
parameter explain
null Used to indicate that a field can be empty.
unique If set to unique=True then the field must be unique in this table.
db_index If db_index=True, it means setting an index for this field.
default Set a default value for this field.
auto_now_add (with time field) Configuration=True, the current time will be added to the database when creating a data record
auto_now (field with time) When configured = True, this field will be updated every time the data record is updated.
choices A list or tuple of two tuples;
the first value in the tuple is the value actually stored in the database, and the second value is the description of the option.
Once the value is set, the form style will display the selection box, and Not a standard text box, the options in the selection box are tuples in choices
Examples of choices:
class TestTable(models.Model):    
	CHAR_CHOICE = [        
		('H',"非常苦难"),        
		('M',"中等难度"),        
		('S',"非常简单"),    ]    
	choicechar = modesl.CharField(max_length=1,choices=CHAR_CHOICE)
field:
 DecimalField(Field)
        - 10进制小数
        - 参数:
            max_digits,小数总长度
            decimal_places,小数位长度




AutoField(Field)
        - int自增列,必须填入参数 primary_key=True
 
    BigAutoField(AutoField)
        - bigint自增列,必须填入参数 primary_key=True
 
        注:当model中如果没有自增列,则自动会创建一个列名为id的列
        from django.db import models
 
        class UserInfo(models.Model):
            # 自动创建一个列名为id的且为自增的整数列
            username = models.CharField(max_length=32)
 
        class Group(models.Model):
            # 自定义自增列
            nid = models.AutoField(primary_key=True)
            name = models.CharField(max_length=32)
 
    SmallIntegerField(IntegerField):
        - 小整数 -32768 ~ 32767
 
    PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField)
        - 正小整数 0 ~ 32767
    IntegerField(Field)
        - 整数列(有符号的) -2147483648 ~ 2147483647
 
    PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField)
        - 正整数 0 ~ 2147483647
 
    BigIntegerField(IntegerField):
        - 长整型(有符号的) -9223372036854775808 ~ 9223372036854775807
 
    BooleanField(Field)
        - 布尔值类型
 
    NullBooleanField(Field):
        - 可以为空的布尔值
 
    CharField(Field)
        - 字符类型
        - 必须提供max_length参数, max_length表示字符长度
 
    TextField(Field)
        - 文本类型
 
    EmailField(CharField):
        - 字符串类型,Django Admin以及ModelForm中提供验证机制
 
    IPAddressField(Field)
        - 字符串类型,Django Admin以及ModelForm中提供验证 IPV4 机制
 
    GenericIPAddressField(Field)
        - 字符串类型,Django Admin以及ModelForm中提供验证 Ipv4和Ipv6
        - 参数:
            protocol,用于指定Ipv4或Ipv6, 'both',"ipv4","ipv6"
            unpack_ipv4, 如果指定为True,则输入::ffff:192.0.2.1时候,可解析为192.0.2.1,开启刺功能,需要protocol="both"
 
    URLField(CharField)
        - 字符串类型,Django Admin以及ModelForm中提供验证 URL
 
    SlugField(CharField)
        - 字符串类型,Django Admin以及ModelForm中提供验证支持 字母、数字、下划线、连接符(减号)
 
    CommaSeparatedIntegerField(CharField)
        - 字符串类型,格式必须为逗号分割的数字
 
    UUIDField(Field)
        - 字符串类型,Django Admin以及ModelForm中提供对UUID格式的验证
 
    FilePathField(Field)
        - 字符串,Django Admin以及ModelForm中提供读取文件夹下文件的功能
        - 参数:
                path,                      文件夹路径
                match=None,                正则匹配
                recursive=False,           递归下面的文件夹
                allow_files=True,          允许文件
                allow_folders=False,       允许文件夹
 
    FileField(Field)
        - 字符串,路径保存在数据库,文件上传到指定目录
        - 参数:
            upload_to = ""      上传文件的保存路径
            storage = None      存储组件,默认django.core.files.storage.FileSystemStorage
 
    ImageField(FileField)
        - 字符串,路径保存在数据库,文件上传到指定目录
        - 参数:
            upload_to = ""      上传文件的保存路径
            storage = None      存储组件,默认django.core.files.storage.FileSystemStorage
            width_field=None,   上传图片的高度保存的数据库字段名(字符串)
            height_field=None   上传图片的宽度保存的数据库字段名(字符串)
 
    DateTimeField(DateField)
        - 日期+时间格式 YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
 
    DateField(DateTimeCheckMixin, Field)
        - 日期格式      YYYY-MM-DD
 
    TimeField(DateTimeCheckMixin, Field)
        - 时间格式      HH:MM[:ss[.uuuuuu]]
 
    DurationField(Field)
        - 长整数,时间间隔,数据库中按照bigint存储,ORM中获取的值为datetime.timedelta类型
 
    FloatField(Field)
        - 浮点型
 
   
 
    BinaryField(Field)
        - 二进制类型
 
#注意:这些操作,如果是直接用数据库操作语句进行添加的话,不会有报错或是警告之类的信息
        #但是如果换到后台管理页面上,进行添加则会有相应的警告!









Guess you like

Origin blog.csdn.net/s_frozen/article/details/129031435