自定义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)

 

posted @ 2019-08-09 19:03  hui1019  阅读(345)  评论(0编辑  收藏  举报