Code

Ticket #5101: validate-on-save.diff

File validate-on-save.diff, 669 bytes (added by anonymous, 7 years ago)
Line 
1--- a/django/db/models/base.py  Fri Jul 27 09:24:19 2007 -0400
2+++ b/django/db/models/base.py  Fri Jul 27 09:29:14 2007 -0400
3@@ -282,6 +282,16 @@ class Model(object):
4                 error_dict[f.name] = errors
5         return error_dict
6 
7+    def validate_on_save(self):
8+        """
9+        perform validation before saving
10+        """
11+        errors = self.validate()
12+        if len(errors):
13+            from django.core import validators
14+            raise validators.ValidationError (errors)
15+        self.save()
16+
17     def _collect_sub_objects(self, seen_objs):
18         """
19         Recursively populates seen_objs with all objects related to this object.