From bfc63a763ae32e0bf25c7087a85fb329ab3f1630 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 5 Aug 2011 09:59:49 -0400 Subject: [PATCH] Django uses Model.clean for custom Model validation. --- cookbook/models.py | 3 ++- cookbook/tests.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cookbook/models.py b/cookbook/models.py index 0726476..cfad7b5 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -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): diff --git a/cookbook/tests.py b/cookbook/tests.py index 4dd06f5..0f3ead8 100644 --- a/cookbook/tests.py +++ b/cookbook/tests.py @@ -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) -- 2.26.2