Reputation: 4818
I am trying to custom validate my model form. For the purpose, I wrote the following code:
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = '__all__'
def clean(self):
batch_start_year = self.cleaned_data.get('batch_start_year',None)
I am getting error like:
'StudentForm' object has no attribute 'get'
I tried another solution, but it didn't work either.
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = '__all__'
def clean(self):
cleaned_data = super(StudentForm, self).clean()
batch_start_year = cleaned_data['batch_start_year']
Please help me to solve this.
Full stack trace:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/shahjahan/Desktop/jmialumniusa/jmialumniusa_app/views.py", line 18, in apply
if form.is_valid():
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py", line 161, in is_valid
return self.is_bound and not self.errors
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py", line 153, in errors
self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py", line 364, in full_clean
self._post_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 377, in _post_clean
exclude = self._get_validation_exclusions()
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 337, in _get_validation_exclusions
field_value = self.cleaned_data.get(field)
AttributeError: 'StudentForm' object has no attribute 'get'
Upvotes: 0
Views: 1287
Reputation: 14391
If you want to validate all your form, you can use clean method like this
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = '__all__'
def clean(self):
cleaned_data = super(StudentForm, self).clean()
batch_start_year = cleaned_data.get('batch_start_year')
In this case, you do not need to return anything. You will just raise validation error. If you want to validate some specific field, you will do it like this
def clean_field_name(self):
data = self.cleaned_data.get('field_name')
if "something" not in data:
raise forms.ValidationError("Validation message")
# Always return the cleaned data, whether you have changed it or
# not.
return data
Another possibility of error can be the way you are sending data to your form
student = Student.objects.get(pk=id)
form = StudentForm(intention) # An unbound form
The first argument to a form is the data but you are passing the instance. To properly pass the instance you should use:
student = Student.objects.get(pk=id)
form = StudentForm(instance=student) #
Upvotes: 0
Reputation: 53326
You should return cleaned data from clean()
method or raise error. You are not doing that.
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = '__all__'
def clean(self):
batch_start_year = self.cleaned_data.get('batch_start_year',None)
# do something
return self.cleaned_data
Upvotes: 1