我有这个代码:
class RegisterForm(forms.Form):
acc_types_lst = AccountType.objects.values_list('Name', flat=True).order_by('ID')
Name = forms.CharField(max_length=255, required=True, label='Имя')
Surname = forms.CharField(max_length=255, required=True, label='Фамилия')
DateOfBirth = forms.DateField(required=True, label='Дата рождения',
input_formats=['%Y-%m-%d', '%m.%d.%Y', '%m.%d.%y'])
Address = forms.CharField(max_length=255, required=True, label='Адрес',
widget=forms.Textarea(attrs={'cols': 55, 'rows': 5}))
Postcode = forms.CharField(required=True, label='Почтовый индекс')
Phone = forms.CharField(max_length=50, required=True, label='Телефон')
PassportID = forms.CharField(max_length=50, required=True, label='Номер паспорта')
AccountType = forms.ChoiceField(choices=[x for x in enumerate(acc_types_lst)],
required=True, label='Тип счета')
DepositeSize = forms.IntegerField(required=True, label='Начальный вклад')
def clean_name(self):
name = re.match('^[a-zA-Z]*$', self.cleaned_data['Name']).group()
if name is None:
raise forms.ValidationError("Имя не корректно или строка пуста!")
else:
return name
...
def clean_deposit(self):
deposite_size = int(self.cleaned_data['DepositeSize'])
acc_type = AccountType.objects.values('Name', 'MinDep').get(ID=1)['MinDep']
if deposite_size < int(acc_type):
raise forms.ValidationError("Тип счета "+acc_type['Name']+" требует минимум "+acc_type['MinDep'] + "!")
else:
return deposite_size
但我无法理解一件事:为什么用户验证器(clean_deposit)不引发异常?是的,我会在“manage.py shell”中检查这个,但在实际情况下(调试模式)系统会通过这个。