summaryrefslogtreecommitdiff
path: root/tests/modeltests/validation/models.py
blob: 1260417426f0c2c9440825cfa607f0dbc2080059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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

def validate_answer_to_universe(value):
    if value != 42:
        raise ValidationError('This is not the answer to life, universe and everything!', code='not42')

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, code='not_equal', params=(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)
    url = models.URLField(blank=True)
    f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe, ValidateFieldNotEqualsOtherField('number')])

    def validate(self):
        super(ModelToValidate, self).validate()
        if self.number == 11:
            raise ValidationError('Invalid number supplied!')

class UniqueFieldsModel(models.Model):
    unique_charfield = models.CharField(max_length=100, unique=True)
    unique_integerfield = models.IntegerField(unique=True)
    non_unique_field = models.IntegerField()

class CustomPKModel(models.Model):
    my_pk_field = models.CharField(max_length=100, primary_key=True)

class UniqueTogetherModel(models.Model):
    cfield = models.CharField(max_length=100)
    ifield = models.IntegerField()
    efield = models.EmailField()

    class Meta:
        unique_together = (('ifield', 'cfield',),('ifield', 'efield'), )

class UniqueForDateModel(models.Model):
    start_date = models.DateField()
    end_date = models.DateTimeField()
    count = models.IntegerField(unique_for_date="start_date", unique_for_year="end_date")
    order = models.IntegerField(unique_for_month="end_date")
    name = models.CharField(max_length=100)

class CustomMessagesModel(models.Model):
    other  = models.IntegerField(blank=True, null=True)
    number = models.IntegerField(
            error_messages={'null': 'NULL', 'not42': 'AAARGH', 'not_equal': '%s != me'},
            validators=[validate_answer_to_universe, ValidateFieldNotEqualsOtherField('other')]
        )