tags:

views:

342

answers:

1

I need to raise an exception in a model's save method. I'm hoping that an exception exists that will be caught by any django ModelForm that uses this model including the admin forms.

I tried raising django.forms.ValidationError, but this seems to be uncaught by the admin forms. The model makes a remote procedure call at save time, and it's not known until this call if the input is valid.

Thanks, Pete

+3  A: 

There's currently no way of performing validation in model save methods. This is however being developed, as a separate model-validation branch, and should be merged into trunk in the next few months.

In the meantime, you need to do the validation at the form level. It's quite simple to create a ModelForm subclass with a clean() method which does your remote call and raises the exception accordingly, and use this both in the admin and as the basis for your other forms.

Daniel Roseman
django has been bumming me out lately. I went with this approach, but I'm not satisfied. Code that modifies a model does not belong in a clean method.
slypete
Just checking in, has this branch you talked about been merged in yet?
rennat