Django uses Model.clean for custom Model validation.
authorW. Trevor King <wking@drexel.edu>
Fri, 5 Aug 2011 13:59:49 +0000 (09:59 -0400)
committerW. Trevor King <wking@drexel.edu>
Fri, 5 Aug 2011 13:59:49 +0000 (09:59 -0400)
cookbook/models.py
cookbook/tests.py

index 072647602421caa84674676c1bc2a4af11fc49c8..cfad7b588687259b968cc7ad87530502b5fdb41a 100644 (file)
@@ -84,7 +84,7 @@ class Amount (models.Model):
                     fmt = u'{0.value} ({0.min_value}-{0.max_value}) {0.unit}'
         return fmt.format(self)
 
-    def validate_amount(self):
+    def clean(self):
         if self.value is None:
             if self.min_value is None and self.max_value is not None:
                 raise ValidationError('cannot only set max_value')
@@ -92,6 +92,7 @@ class Amount (models.Model):
                 raise ValidationError('cannot only set min_value')
         if self.value is not None and self.unit is None:
             raise ValidationError('values must have units')
+        super(Amount, self).clean()
 
 
 class Directions (models.Model):
index 4dd06f5ffd3d38ce10f04f8109b5bbddf6966558..0f3ead8eac8198b8fde633a26df0f08f24cd773c 100644 (file)
@@ -91,6 +91,6 @@ class AmountTest(TestCase):
             self.amount.min_value = minv
             self.amount.max_value = maxv
             if valid:
-                self.amount.validate_amount()
+                self.amount.clean()
             else:
-                self.assertRaises(ValidationError, self.amount.format_amount)
+                self.assertRaises(ValidationError, self.amount.clean)