diff options
| author | Honza Král <honza.kral@gmail.com> | 2009-07-19 20:55:58 +0000 |
|---|---|---|
| committer | Honza Král <honza.kral@gmail.com> | 2009-07-19 20:55:58 +0000 |
| commit | 8d4417c75df30502f47abf6bd3582dfa2425f562 (patch) | |
| tree | ee2fdc4498516ec88b6b97d241e82ca1b8bb9968 /django | |
| parent | 021ad1a02433f194ecd7776aa0522a7cf3b0d9b8 (diff) | |
[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
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/base.py | 15 |
1 files changed, 15 insertions, 0 deletions
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() |
