Custom fields 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 ()
     # use custom fields defined 
    info = MyCharField (= 32 MAX_LENGTH, null = True)   # change field can be empty 



choices = ((. 1, ' M ' ), (2, ' F ' ), (3, ' other ' )) Gender = models.IntegerField (choices = choices, default = 2)

 

Guess you like

Origin www.cnblogs.com/HUIWANG/p/11144571.html