summaryrefslogtreecommitdiff
path: root/tests/modeltests/validation/models.py
diff options
context:
space:
mode:
authorHonza Král <honza.kral@gmail.com>2009-07-19 20:55:58 +0000
committerHonza Král <honza.kral@gmail.com>2009-07-19 20:55:58 +0000
commit8d4417c75df30502f47abf6bd3582dfa2425f562 (patch)
treeee2fdc4498516ec88b6b97d241e82ca1b8bb9968 /tests/modeltests/validation/models.py
parent021ad1a02433f194ecd7776aa0522a7cf3b0d9b8 (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 'tests/modeltests/validation/models.py')
-rw-r--r--tests/modeltests/validation/models.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py
index e41fc3b332..c00b7f28d2 100644
--- a/tests/modeltests/validation/models.py
+++ b/tests/modeltests/validation/models.py
@@ -1,6 +1,7 @@
from datetime import datetime
from django.core.exceptions import ValidationError
+from django.core.validators import ComplexValidator
from django.db import models
from django.test import TestCase
@@ -8,13 +9,21 @@ def validate_answer_to_universe(value):
if value != 42:
raise ValidationError('This is not the answer to life, universe and everything!')
+class ValidateFieldNotEqualsOtherField(ComplexValidator):
+ def __init__(self, other_field):
+ self.other_field = other_field
+
+ def __call__(self, value, all_values={}, obj=None):
+ if value == self.get_value(self.other_field, all_values, obj):
+ raise ValidationError("Must not equal to %r's value" % self.other_field)
+
class ModelToValidate(models.Model):
name = models.CharField(max_length=100)
created = models.DateTimeField(default=datetime.now)
number = models.IntegerField()
parent = models.ForeignKey('self', blank=True, null=True)
email = models.EmailField(blank=True)
- f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe])
+ f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe, ValidateFieldNotEqualsOtherField('number')])
def validate(self):
super(ModelToValidate, self).validate()