Re: Raise field error in models clean method

2013-05-20 Thread Jani Tiainen
On Sun, 19 May 2013 06:38:50 -0700 (PDT)
Kai Schlamp  wrote:

> 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)?

Well if you consider that error you describe is about combination of fields A 
and B how you can tell that which one is really incorrect? A or B? In most 
cases it's really impossible to tell the difference hence it's more natural to 
report that field combination didn't validated and some action must be done to 
correct the situation. 

So in example case end user has to change field A or field B or even both. 

-- 
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+unsubscr...@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.




Raise field error in models clean method

2013-05-19 Thread Kai Schlamp
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+unsubscr...@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.