我是django / python的初学者。我在我的django模型中创建了一个用于存储用户电话号码的字段。我提供了一个正则表达式验证器来验证用户输入的条目,但是当我提交表单时,它会给出多次重复错误。我不确定为什么会出现这个错误。任何帮助表示赞赏。
以下是模型:
class Contact(models.Model):
account_company_name = models.ForeignKey('Account', on_delete=models.CASCADE, db_column='account_id')
name = models.CharField(db_column='Contact name', max_length=120, blank=False, null=False)
email = models.EmailField(primary_key=True, db_column='Email', blank=False, null=False)
phone = models.CharField(primary_key=True, max_length=10,validators = [RegexValidator(r'\d{10}+|\d{5}([- ]*)\d{6}')],\
db_column='Phone', blank=False, null=False)
notes = models.TextField()
class Meta:
db_table = 'Contact'
这是forms.py文件:
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ['account_company_name', 'name', 'email', 'phone', 'notes']
以下是错误跟踪请求:
Traceback:
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "G:\PycharmProjects\crm\crm_app\views.py" in add_contact
43. if form.is_valid():
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\forms\forms.py" in is_valid
183. return self.is_bound and not self.errors
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\forms\forms.py" in errors
175. self.full_clean()
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\forms\forms.py" in full_clean
386. self._post_clean()
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\forms\models.py" in _post_clean
396. self.instance.full_clean(exclude=exclude, validate_unique=False)
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\db\models\base.py" in full_clean
1226. self.clean_fields(exclude=exclude)
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\db\models\base.py" in clean_fields
1268. setattr(self, f.attname, f.clean(raw_value, self))
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\db\models\fields\__init__.py" in clean
603. self.run_validators(value)
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\db\models\fields\__init__.py" in run_validators
555. v(value)
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\core\validators.py" in __call__
60. if not (self.inverse_match is not bool(self.regex.search(
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\utils\functional.py" in inner
238. self._setup()
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\utils\functional.py" in _setup
385. self._wrapped = self._setupfunc()
File "C:\Users\admin_hp\crm_env\lib\site-packages\django\core\validators.py" in _compile
24. return re.compile(regex, flags)
File "C:\Users\admin_hp\crm_env\lib\re.py" in compile
194. return _compile(pattern, flags)
File "C:\Users\admin_hp\crm_env\lib\re.py" in _compile
251. raise error, v # invalid expression
Exception Type: error at /crmbasic/addcontact/
Exception Value: multiple repeat
答案 0 :(得分:0)
你的正则表达式无效。我不知道您所需的格式,但可能是您正在寻找的
phone = models.CharField(
primary_key=True,
max_length=12,
validators=RegexValidator(r"^\d{10}|(\d{5}[\-]\d{6})$")],
db_column='Phone',
blank=False,
null=False)