ORM中自定义一个char类型字段
自定义一个char类型字段
class MyCharField(models.Field): """ 自定义的char类型的字段类 """ def __init__(self, max_length, *args, **kwargs): self.max_length = max_length super(MyCharField, self).__init__(max_length=max_length, *args, **kwargs) def db_type(self, connection): """ 限定生成数据库表的字段类型为char,长度为max_length指定的值 """ return 'char(%s)' % self.max_length
class Person(models.Model):
name = models.CharField(max_length=24)
age = models.IntegerField() # 最大10位
# gender =
birth = models.DateTimeField(auto_now_add=True)
phonenum = MyCharField(max_length=11)