Sunday, May 19, 2013

Raise field error in models clean method

Hello.

How to raise a `ValidationException` in the models `clean` method?

    def clean(self):
        from django.core.exceptions import ValidationError
        raise ValidationError({'title': 'not ok'})

The above does not add the error to the `title` field (when using a form), but to the non field errors.

This is possible inside a form (`self._errors['title'] = self.error_class([msg])`), but `self._errors` does not exist inside the models `clean` method.

I know that clean normally is for non field errors, but there are cases when only some kind of combination of values in separate fields are valid. I want to directly associate the error with a field and let the user know "if you change this field than everything passes".

Is it possible (even if not recommended)?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate