I'm a little stumped here, I can't find what I'm looking for in the Django docs...
What I want, is to be able to override the Save method of an Model. I want it to check for a certain set of conditions - if these conditions are met, i开发者_JS百科t will create the object just fine, but if the conditions are not met, I want to raise an error. The main thing is that I am using the Admin interface for most of these, so I this isn't an error that I will catch myself - this is an error that I need the admin interface to catch and display to the user.
How might I go about doing this chaps? Is there documentation I am missing out on reading? Oh, also to note, I am using Django 1.1, and thus, cannot override the clean / full_clean methods introduced by Django 1.2.
Thanks! Shawn
I'm not 100% sure, but I think you should be able to raise either a ValueError or a django.forms.ValidationError in your save() method in your model
def save(self):
if yourvalidation:
super(Model, self).save() #call super to actually do save
else:
raise #either ValueError or ValidationException
Again, i'm not sure if this will work in 1.1... but this is what I would try.
I want ... to check for a certain set of conditions - if these conditions are met, it will create the object just fine, but if the conditions are not met, I want to raise an error.
This is what a ModelForm is for.
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
精彩评论