From 8d4417c75df30502f47abf6bd3582dfa2425f562 Mon Sep 17 00:00:00 2001 From: Honza Král Date: Sun, 19 Jul 2009 20:55:58 +0000 Subject: [soc2009/model-validation] Added capacity for ComplexValidator handling to models git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11271 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'django') diff --git a/django/db/models/base.py b/django/db/models/base.py index 0e1587c033..f4a1c28179 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -10,6 +10,7 @@ except NameError: import django.db.models.manager # Imported to register signal handler. from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS +from django.core import validators from django.db.models.fields import AutoField, FieldDoesNotExist from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField from django.db.models.query import delete_objects, Q @@ -760,6 +761,20 @@ class Model(object): setattr(self, f.attname, f.clean(getattr(self, f.attname), self)) except ValidationError, e: errors[f.name] = e.messages + + # run complex validators after the fields have been cleaned since they + # need access to model_instance. + for f in self._meta.fields: + if f.name in errors: + continue + + value = getattr(self, f.attname) + for v in f.validators: + if isinstance(v, validators.ComplexValidator): + try: + v(value, obj=self) + except ValidationError, e: + errors.setdefault(f.name, []).extend(e.messages) try: # TODO: run this only if not errors?? self.validate() -- cgit v1.3