django-ORM-常用字段,常用参数,索引,联合唯一索引,联合索引(不唯一)

class User(models.Model):
    #一.常用字段:

    #1.字符字段
    username = models.CharField(max_length=32)

    #2.数字字段
    age = models.IntegerField()#整数
    num = models.DecimalField(max_digits=10,decimal_places=2)#小数,长度10,小数点位数2

    #3.时间字段
    ctime = models.DateTimeField()
    # 时间字段通过models.User.objects.create(ctime='2020-4-29')来添加数据

    #4.枚举,只有这几种颜色可以选择
    color_list = (
        (1,'黑色'),
        (2,'白色'),
        (3,'蓝色')
    )
    color = models.IntegerField(choices=color_list)

    #二.常用参数:
    null = True
    default = xx
    max_length = 32
    db_index = True #普通索引
    unique = True #唯一索引

    #class Meta是固定写法,并且必须写在class User里面,只要写在它里面就可以起作用。
    class Meta:
        #联合唯一索引
        unique_together = (
            ('username','age'),
        )
        #联合索引(不唯一)
        index_together = (
            ('username', 'age'),
        )

猜你喜欢

转载自www.cnblogs.com/liudaya/p/12915273.html