Django - How best to handle ValidationErrors after form.save(commit=False)

Posted by orokusaki on Stack Overflow See other posts from Stack Overflow or by orokusaki
Published on 2010-03-31T02:01:55Z Indexed on 2010/03/31 2:03 UTC
Read the original article Hit count: 1000

This is a fragment of my code from a view:

    if form.is_valid():
        instance = form.save(commit=False)
        try:
            instance.account = request.account
            instance.full_clean()
        except ValidationError, e:
            # Do something with the errors here... I don't know what the best thing to do here is, but I certainly don't want to do it 180 times.

This is an utter mess. Who would want to handle validation errors manually in every view. If you're not modifying the instance after save(commit=False), you don't have to worry about this, but what about in my case where every model has a foreign key to account which is set behind the scenes and hidden from the user?

Any help is really appreciated.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-validation