我试图覆盖要检查的表单的clean()
方法,IBAN属性是否唯一。每个user
都可以拥有IBAN。出于某种原因,django说形式没有属性IBAN,这是不正确的。正如您所看到的,它是表单的第一个属性。
你知道什么问题吗?
class TranslatorRegistrationForm(forms.Form):
IBAN = forms.CharField(max_length=40, required=True)
first_name = forms.CharField(max_length=40, required=True)
last_name = forms.CharField(max_length=40, required=True)
languages = forms.ModelMultipleChoiceField(Language.objects.all(), label='Languages: ',
help_text="You can choose from UNKNOWN levels, to gain level, you will be tested")
def __init__(self,user,*args, **kwargs):
super(TranslatorRegistrationForm, self).__init__(*args, **kwargs)
self.user = user
def clean(self):
cleaned_data = super(TranslatorRegistrationForm, self).clean()
if len(UserProfile.objects.filter(IBAN=self.IBAN).exclude(user=self.user))>0:
raise ValidationError
return cleaned_data
TRACEBACK:
环境:
Request Method: POST
Request URL: http://127.0.0.1:8000/register-as-translator/
Django Version: 1.8.12
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'SolutionsForLanguagesApp',
'crispy_forms',
'super_inlines',
'django_tables2',
'language_tests',
'smart_selects',
'django_extensions',
'constance',
'constance.backends.database',
'nested_inline')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.locale.LocaleMiddleware')
Traceback:
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\SolutionsForLanguagesApp\views.py" in register_as_translator
110. if register_as_translator_form.is_valid():
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\forms.py" in is_valid
184. return self.is_bound and not self.errors
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\forms.py" in errors
176. self.full_clean()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\forms.py" in full_clean
393. self._clean_form()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\forms.py" in _clean_form
417. cleaned_data = self.clean()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\SolutionsForLanguagesApp\forms.py" in clean
116. if len(UserProfile.objects.filter(IBAN=self.IBAN).exclude(user=self.user))>0:
Exception Type: AttributeError at /register-as-translator/
Exception Value: 'TranslatorRegistrationForm' object has no attribute 'IBAN'
答案 0 :(得分:3)
由于您是validating the value of a single field iban
,最好定义一个clean_iban
方法而不是clean
:
def clean_iban(self):
iban = self.cleaned_data['IBAN']
# Note using exists() is more efficient and pythonic than 'len() > 0'
if UserProfile.objects.filter(IBAN=iban).exists():
raise ValidationError('Invalid IBAN')
return iban
clean
方法适用于validating fields that depend on each other。如果您覆盖clean
,则无法假设某个值位于cleaned_data
。
def clean(self):
cleaned_data = super(TranslatorRegistrationForm, self).clean()
if 'iban' in cleaned_data:
iban = cleaned_data['iban']
if len(UserProfile.objects.filter(IBAN=self.IBAN).exclude(user=self.user))>0:
raise ValidationError('Invalid IBAN')
return cleaned_data
答案 1 :(得分:1)
在您调用super(TranslatorRegistrationForm, self).clean()
后,您应该能够访问cleaned_data['IBAN']
中的IBAN字段的值。属性self.IBAN
是字段定义,而不是字段的值。