Unidentified Errors.Please notify... error instead of my simple forms.ValidationError
OK, trying to add a clean method to a ModelForm in Django. I'm adding a simple raise statement just to see if it works, and instead of my message, I get "Unidentified Errors. Please notify..."
Here's my (simple) test:
class ConfigurationForm(forms.ModelForm):
...
def clean(self):
cleaned_data = self.cleaned_data
typeid = cleaned_data.get("typeid")
value = cleaned_data.get("value")
开发者_开发问答 if value and typeid:
raise forms.ValidationError("this is the error")
I couldn't even find a reference to "Unidentified Errors" anywhere in the django code base. Thanks in advance for your help.
Is your clean method returning the cleaned_data? If you look at the docs here:
https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
you'll notice you have to make sure to return self.cleaned_data. Furthermore, don't raise validation errors; instead delete the invalid field from the data (again, as you have to return it)
So:
def clean(self):
typeid = cleaned_data.get("typeid", False)
value = cleaned_data.get("value", False)
if value and typeid:
self._errors["typeid"] = self.error_class(["Some error has happened"])
del(self.cleaned_data['typeid'])
return self.cleaned_data
I use clean model methond intensively.
You should search for your errors on NON_FIELD_ERRORS:
On template:
{% if form.non_field_errors %}
<ul>
{% for error in form.non_field_errors %}
<li> {{error}} </li>
{% endfor %}
</ul>
{% endif %}
On code:
form._errors.[NON_FIELD_ERRORS]
Here you can learn about how to move business rules from forms to model.
精彩评论