Hi,

Thank you Carl and Tim for your replies. I've created a ticket #23865 and written a patch [1]for the Model.clean() docs [1].

On 17/11/14 16:53, Carl Meyer wrote:
On 11/17/2014 09:48 AM, Tim Graham wrote:
The reasoning for why it wasn't documented is provided here:
https://github.com/django/django/pull/1443#issuecomment-28045150

That comment is actually in favor of documenting it for Model.clean(),
just suggested it be done in a separate PR.

(Personally I think it would be better to document the same technique
for both Model.clean() and Form.clean(), because I don't think
Form.add_error() is significantly easier, and it breaks the API
consistency of "always flag a validation problem by raising
ValidationError", but I guess that's a larger discussion.)

I've been updating a codebase to Django 1.7 this week. Personally, I find the add_error API to be more concise when you don't want to raise an exception immediately.

class MyForm(forms.Form):
    def clean():
        if check1():
            self.add_error('field1', 'error message')
        if check2():
            self.add_error('field2', 'error message')

In my models' clean methods, I have to keep track of the errors as I go along.

class MyModel(models.Model):
    def clean():
        errors = {}
        if check1():
            errors['field1'] = 'error message'
        if check2():
            errors['field2'] = 'error message'
        if errors:
            raise ValidationError(errors)

Having said that, it's not that much more difficult, only 3 extra lines of code in this case. API consistency is a good argument for documenting the ValidationError(dict) approach in both places.

Cheers,
Alasdair

--
Alasdair Nicol
Developer, MEMSET

mail: [email protected]
 web: http://www.memset.com/

Memset Ltd., registration number 4504980.
Building 87, Dunsfold Park, Stovolds Hill, Cranleigh, Surrey, GU6 8TB, UK

--
You received this message because you are subscribed to the Google Groups "Django 
developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/546B9F58.7070108%40memset.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to