jmagnusson
jmagnusson

Reputation: 5869

Raise ValidationError from pre_save receiver function?

I'd like to inform the user if something fails during processing of the data in instance in my pre_save receiver function.

Is it possible to raise a custom ValidationError from the receiver function? If not, how would I go about implementing something like this?

Upvotes: 14

Views: 4248

Answers (2)

Jocelyn delalande
Jocelyn delalande

Reputation: 5384

You can raise whatever exception you want within the pre_save listener function, it will propagate to your save() call and prevent it from suceeding.

It will bubble up to where you call save(), from here, you can catch it (try/except…) and notify the user.

Upvotes: 9

Dima Bildin
Dima Bildin

Reputation: 1553

I guess you should either use Model.clean() method (http://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#django.db.models.Model.clean) or form clean http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-clean-method.

Upvotes: 0

Related Questions