2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2006-06-01 03:23:07 +08:00
|
|
|
30. Validation
|
2006-06-01 03:10:43 +08:00
|
|
|
|
|
|
|
This is an experimental feature!
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
Each model instance has a validate() method that returns a dictionary of
|
|
|
|
validation errors in the instance's fields. This method has a side effect
|
|
|
|
of converting each field to its appropriate Python data type.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
is_child = models.BooleanField()
|
|
|
|
name = models.CharField(maxlength=20)
|
|
|
|
birthdate = models.DateField()
|
|
|
|
favorite_moment = models.DateTimeField()
|
|
|
|
email = models.EmailField()
|
|
|
|
|
2006-06-04 08:23:51 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> import datetime
|
|
|
|
>>> valid_params = {
|
|
|
|
... 'is_child': True,
|
|
|
|
... 'name': 'John',
|
|
|
|
... 'birthdate': datetime.date(2000, 5, 3),
|
|
|
|
... 'favorite_moment': datetime.datetime(2002, 4, 3, 13, 23),
|
|
|
|
... 'email': 'john@example.com'
|
|
|
|
... }
|
|
|
|
>>> p = Person(**valid_params)
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, id='23'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.id
|
|
|
|
23
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, id='foo'))
|
|
|
|
>>> p.validate()
|
|
|
|
{'id': ['This value must be an integer.']}
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, id=None))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> repr(p.id)
|
|
|
|
'None'
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, is_child='t'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.is_child
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, is_child='f'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.is_child
|
|
|
|
False
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, is_child=True))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.is_child
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, is_child=False))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.is_child
|
|
|
|
False
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, is_child='foo'))
|
|
|
|
>>> p.validate()
|
|
|
|
{'is_child': ['This value must be either True or False.']}
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, name=u'Jose'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.name
|
|
|
|
u'Jose'
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, name=227))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.name
|
|
|
|
'227'
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3)))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.birthdate
|
|
|
|
datetime.date(2000, 5, 3)
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, birthdate=datetime.datetime(2000, 5, 3)))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.birthdate
|
|
|
|
datetime.date(2000, 5, 3)
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, birthdate='2000-05-03'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.birthdate
|
|
|
|
datetime.date(2000, 5, 3)
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, birthdate='2000-5-3'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.birthdate
|
|
|
|
datetime.date(2000, 5, 3)
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, birthdate='foo'))
|
|
|
|
>>> p.validate()
|
|
|
|
{'birthdate': ['Enter a valid date in YYYY-MM-DD format.']}
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3, 13, 23)))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.favorite_moment
|
|
|
|
datetime.datetime(2002, 4, 3, 13, 23)
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3)))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.favorite_moment
|
|
|
|
datetime.datetime(2002, 4, 3, 0, 0)
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, email='john@example.com'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.email
|
|
|
|
'john@example.com'
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, email=u'john@example.com'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.email
|
|
|
|
u'john@example.com'
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, email=22))
|
|
|
|
>>> p.validate()
|
|
|
|
{'email': ['Enter a valid e-mail address.']}
|
|
|
|
|
2007-02-26 13:07:12 +08:00
|
|
|
# Make sure that Date and DateTime return validation errors and don't raise Python errors.
|
|
|
|
>>> Person(name='John Doe', is_child=True, email='abc@def.com').validate()
|
|
|
|
{'favorite_moment': ['This field is required.'], 'birthdate': ['This field is required.']}
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|