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

相关文章:

  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2021-12-16
  • 2022-02-05
  • 2021-12-03
  • 2022-01-05
  • 2022-03-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2021-12-12
  • 2022-02-11
  • 2021-12-04
相关资源
相似解决方案