This is a bit of a hack, but as far as I can see currently the only way to specify a validation error that is specific to a field in form.clean().
I am aware of clean_<fieldname>, but those are difficult to use when the validation process for a field involves other fields as well, because the necessary data might at that point not be yet available in form.cleaned_data.
1 2 3 4 5 6 7 8 | def clean(self):
try:
# do validation here
except ValidationError, e:
if blame_field:
self._errors[blame_field] = e.messages
else:
raise e
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
This is how you can blame different fields for different errors:
#
oh.. I forgot to return the cleaned dictionary at the end. so there should be
after all.
#
Archatas, instead of: self._errors['field1'] = self._errors.get('field1', [])
you should do: self._errors['field1'] = self._errors.get('field1', ErrorList())
ErrorList comes from django.forms.utils
#
Please login first before commenting.