自定义orm字段

class MyCharField(models.Field):
    def __init__(self,max_length,*args,**kwargs):
        self.max_length = max_length
        super().__init__(max_length=max_length,*args,**kwargs)

    def db_type(self, connection):
        return 'char(%s)'%self.max_length

class Product(models.Model):
    name = models.CharField(max_length=32)  # 都是类实例化出来的对象
    price = models.DecimalField(max_digits=8,decimal_places=2)
    maichu = models.IntegerField()
    kucun = models.IntegerField()
    #使用自定义的字段
    info = MyCharField(max_length=32,null=True)  # 改字段可以为空



choices = ((1,''),(2,''),(3,'其他')) gender = models.IntegerField(choices=choices,default=2)

猜你喜欢

转载自www.cnblogs.com/HUIWANG/p/11144571.html