2006-05-02 01:31:56 +00:00
|
|
|
"""
|
2007-03-23 20:17:04 +00:00
|
|
|
31. Validation
|
2006-05-31 19:10:43 +00:00
|
|
|
|
|
|
|
This is an experimental feature!
|
2006-05-02 01:31:56 +00: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()
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=20)
|
2006-05-02 01:31:56 +00:00
|
|
|
birthdate = models.DateField()
|
|
|
|
favorite_moment = models.DateTimeField()
|
|
|
|
email = models.EmailField()
|
2008-07-29 05:09:29 +00:00
|
|
|
best_time = models.TimeField()
|
2006-05-02 01:31:56 +00:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.name
|
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 01:31:56 +00: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),
|
2008-07-29 05:09:29 +00:00
|
|
|
... 'email': 'john@example.com',
|
|
|
|
... 'best_time': datetime.time(16, 20),
|
2006-05-02 01:31:56 +00:00
|
|
|
... }
|
|
|
|
>>> 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'))
|
2008-03-19 19:11:51 +00:00
|
|
|
>>> p.validate()['id']
|
|
|
|
[u'This value must be an integer.']
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
>>> 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'))
|
2008-03-19 19:11:51 +00:00
|
|
|
>>> p.validate()['is_child']
|
|
|
|
[u'This value must be either True or False.']
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
>>> 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
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
u'227'
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
>>> 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'))
|
2008-03-19 19:11:51 +00:00
|
|
|
>>> p.validate()['birthdate']
|
|
|
|
[u'Enter a valid date in YYYY-MM-DD format.']
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
>>> 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)
|
|
|
|
|
2008-07-29 05:09:29 +00:00
|
|
|
>>> p = Person(**dict(valid_params, best_time='16:20:00'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.best_time
|
|
|
|
datetime.time(16, 20)
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, best_time='16:20'))
|
|
|
|
>>> p.validate()
|
|
|
|
{}
|
|
|
|
>>> p.best_time
|
|
|
|
datetime.time(16, 20)
|
|
|
|
|
|
|
|
>>> p = Person(**dict(valid_params, best_time='bar'))
|
|
|
|
>>> p.validate()['best_time']
|
|
|
|
[u'Enter a valid time in HH:MM[:ss[.uuuuuu]] format.']
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> 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))
|
2008-03-19 19:11:51 +00:00
|
|
|
>>> p.validate()['email']
|
|
|
|
[u'Enter a valid e-mail address.']
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2007-02-26 05:07:12 +00:00
|
|
|
# Make sure that Date and DateTime return validation errors and don't raise Python errors.
|
2008-03-19 19:11:51 +00:00
|
|
|
>>> p = Person(name='John Doe', is_child=True, email='abc@def.com')
|
|
|
|
>>> errors = p.validate()
|
|
|
|
>>> errors['favorite_moment']
|
|
|
|
[u'This field is required.']
|
|
|
|
>>> errors['birthdate']
|
|
|
|
[u'This field is required.']
|
2008-07-29 05:09:29 +00:00
|
|
|
>>> errors['best_time']
|
|
|
|
[u'This field is required.']
|
2007-02-26 05:07:12 +00:00
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
"""}
|