【问题标题】:__init__() got an unexpected keyword argument 'min_length' [duplicate]__init__() 有一个意外的关键字参数“min_length”[重复]
【发布时间】:2017-08-24 10:19:37
【问题描述】:

我收到以下错误:

...
File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/luowensheng/Desktop/QIYUN/Project/officialWeb/frontend/models.py", line 93, in <module>
    class User(models.Model):
  File "/Users/luowensheng/Desktop/QIYUN/Project/officialWeb/frontend/models.py", line 97, in User
    phone = models.CharField(min_length=11, max_length=11)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/db/models/fields/__init__.py", line 1061, in __init__
    super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'min_length'

我的型号代码如下:

class User(models.Model):
    username = models.CharField(max_length=16)
    password = models.CharField(max_length=16)
    real_name = models.CharField(max_length=12)
    phone = models.CharField(min_length=11, max_length=11) # this is the line 97 
    email = models.EmailField()
    qq = models.CharField(max_length=10)
    address = models.CharField(max_length=64)  
    id_card = models.CharField(min_length = 18, max_length=18)
    id_card_img_front = models.CharField(max_length=256)
    id_card_img_back = models.CharField(max_length=256)
    nickname = models.CharField(max_length=16)
    profile = models.CharField(max_length=256)

为什么会出现这个错误? 顺便说一句,是否有更好的方法将电话长度限制为11

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    正如错误提示,CharField 没有min_length 选项。你可以改用MinLengthValidator

    from django.core.validators import MinLengthValidator
    
    class User(models.Model):
        phone = models.CharField(max_length=11, validators=[MinLengthValidator(11)])
    

    【讨论】:

    • 谢谢,顺便问一下有没有更好的方法将 CharField 长度限制为11
    • @aircraft 为固定长度,您可以在此处阅读更多内容stackoverflow.com/questions/2470760/…
    • 这是我能想到的最简单的方法。由于您希望长度恰好为 11,因此您可以 write your own validator 提供更具体的错误消息。
    【解决方案2】:

    CharField 数据库模型字段实例只有一个 max_length 参数。这可能是因为 SQL 中只有一个最大字符长度约束等效项。

    from django.core.validators import RegexValidator
    
    class User(models.Model):
        phone = models.CharField(validators=[RegexValidator(regex='^.{11}$', message='Length has to be 11', code='nomatch')])
    

    【讨论】:

      猜你喜欢
      • 2018-04-26
      • 1970-01-01
      • 2019-01-05
      • 2021-10-29
      • 2014-09-14
      • 2015-01-27
      • 2016-08-25
      • 2021-09-20
      • 1970-01-01
      相关资源
      最近更新 更多