mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
ComplexValidators are not yet run on the model and custom message overring is still not in place git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11037 bcc190cf-cafb-0310-a4f2-bffc1f526a37
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
from datetime import datetime
|
|
|
|
from django.core.exceptions import ValidationError
|
|
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!')
|
|
|
|
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])
|
|
|
|
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)
|