Hey all,

I have a model with some fairly strict rules on what is or isn't allowed 
and I'd like to perform some validation. The model is created via various 
forms, other functions, and via api calls and so validating the data before 
the model is created would be quite complicated. Reading on Stack Overflow, 
people seem to caution against overriding the Model.save() function to call 
a clean function which checks for these issues. However all the posts I've 
seen on it were from Django 1.2 or 1.3 and I'm not certain if they're still 
relevant. So Is there a reason not to do it this way? If so, what would a 
good solution be.

Example Code:
class MyModel:
    
    def save(self, *args, **kwargs):
        """
        Override the save function to provide some validation on the model.
        """
        self.clean()
        super(MyModel, self).save(*args, **kwargs)


    def clean(self):
        if badThing == True:

            raise ValidationError("This data is bad and you should feel bad"
)
   

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b992f985-d640-4247-a291-f0932cd7ade8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to