我收到了轰鸣声错误:
...
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
?
答案 0 :(得分:2)
如错误所示,CharField
没有min_length
选项。您可以使用MinLengthValidator
代替
from django.core.validators import MinLengthValidator
class User(models.Model):
phone = models.CharField(max_length=11, validators=[MinLengthValidator(11)])
答案 1 :(得分:1)
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')])