thirdparty/google_appengine/lib/django/tests/modeltests/validation/models.py
changeset 2866 a04b1e4126c4
parent 2864 2e0b0af889be
child 2868 9f7f269383f7
equal deleted inserted replaced
2864:2e0b0af889be 2866:a04b1e4126c4
     1 """
       
     2 31. Validation
       
     3 
       
     4 This is an experimental feature!
       
     5 
       
     6 Each model instance has a validate() method that returns a dictionary of
       
     7 validation errors in the instance's fields. This method has a side effect
       
     8 of converting each field to its appropriate Python data type.
       
     9 """
       
    10 
       
    11 from django.db import models
       
    12 
       
    13 class Person(models.Model):
       
    14     is_child = models.BooleanField()
       
    15     name = models.CharField(maxlength=20)
       
    16     birthdate = models.DateField()
       
    17     favorite_moment = models.DateTimeField()
       
    18     email = models.EmailField()
       
    19 
       
    20     def __str__(self):
       
    21         return self.name
       
    22 
       
    23 __test__ = {'API_TESTS':"""
       
    24 
       
    25 >>> import datetime
       
    26 >>> valid_params = {
       
    27 ...     'is_child': True,
       
    28 ...     'name': 'John',
       
    29 ...     'birthdate': datetime.date(2000, 5, 3),
       
    30 ...     'favorite_moment': datetime.datetime(2002, 4, 3, 13, 23),
       
    31 ...     'email': 'john@example.com'
       
    32 ... }
       
    33 >>> p = Person(**valid_params)
       
    34 >>> p.validate()
       
    35 {}
       
    36 
       
    37 >>> p = Person(**dict(valid_params, id='23'))
       
    38 >>> p.validate()
       
    39 {}
       
    40 >>> p.id
       
    41 23
       
    42 
       
    43 >>> p = Person(**dict(valid_params, id='foo'))
       
    44 >>> p.validate()
       
    45 {'id': ['This value must be an integer.']}
       
    46 
       
    47 >>> p = Person(**dict(valid_params, id=None))
       
    48 >>> p.validate()
       
    49 {}
       
    50 >>> repr(p.id)
       
    51 'None'
       
    52 
       
    53 >>> p = Person(**dict(valid_params, is_child='t'))
       
    54 >>> p.validate()
       
    55 {}
       
    56 >>> p.is_child
       
    57 True
       
    58 
       
    59 >>> p = Person(**dict(valid_params, is_child='f'))
       
    60 >>> p.validate()
       
    61 {}
       
    62 >>> p.is_child
       
    63 False
       
    64 
       
    65 >>> p = Person(**dict(valid_params, is_child=True))
       
    66 >>> p.validate()
       
    67 {}
       
    68 >>> p.is_child
       
    69 True
       
    70 
       
    71 >>> p = Person(**dict(valid_params, is_child=False))
       
    72 >>> p.validate()
       
    73 {}
       
    74 >>> p.is_child
       
    75 False
       
    76 
       
    77 >>> p = Person(**dict(valid_params, is_child='foo'))
       
    78 >>> p.validate()
       
    79 {'is_child': ['This value must be either True or False.']}
       
    80 
       
    81 >>> p = Person(**dict(valid_params, name=u'Jose'))
       
    82 >>> p.validate()
       
    83 {}
       
    84 >>> p.name
       
    85 u'Jose'
       
    86 
       
    87 >>> p = Person(**dict(valid_params, name=227))
       
    88 >>> p.validate()
       
    89 {}
       
    90 >>> p.name
       
    91 '227'
       
    92 
       
    93 >>> p = Person(**dict(valid_params, birthdate=datetime.date(2000, 5, 3)))
       
    94 >>> p.validate()
       
    95 {}
       
    96 >>> p.birthdate
       
    97 datetime.date(2000, 5, 3)
       
    98 
       
    99 >>> p = Person(**dict(valid_params, birthdate=datetime.datetime(2000, 5, 3)))
       
   100 >>> p.validate()
       
   101 {}
       
   102 >>> p.birthdate
       
   103 datetime.date(2000, 5, 3)
       
   104 
       
   105 >>> p = Person(**dict(valid_params, birthdate='2000-05-03'))
       
   106 >>> p.validate()
       
   107 {}
       
   108 >>> p.birthdate
       
   109 datetime.date(2000, 5, 3)
       
   110 
       
   111 >>> p = Person(**dict(valid_params, birthdate='2000-5-3'))
       
   112 >>> p.validate()
       
   113 {}
       
   114 >>> p.birthdate
       
   115 datetime.date(2000, 5, 3)
       
   116 
       
   117 >>> p = Person(**dict(valid_params, birthdate='foo'))
       
   118 >>> p.validate()
       
   119 {'birthdate': ['Enter a valid date in YYYY-MM-DD format.']}
       
   120 
       
   121 >>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3, 13, 23)))
       
   122 >>> p.validate()
       
   123 {}
       
   124 >>> p.favorite_moment
       
   125 datetime.datetime(2002, 4, 3, 13, 23)
       
   126 
       
   127 >>> p = Person(**dict(valid_params, favorite_moment=datetime.datetime(2002, 4, 3)))
       
   128 >>> p.validate()
       
   129 {}
       
   130 >>> p.favorite_moment
       
   131 datetime.datetime(2002, 4, 3, 0, 0)
       
   132 
       
   133 >>> p = Person(**dict(valid_params, email='john@example.com'))
       
   134 >>> p.validate()
       
   135 {}
       
   136 >>> p.email
       
   137 'john@example.com'
       
   138 
       
   139 >>> p = Person(**dict(valid_params, email=u'john@example.com'))
       
   140 >>> p.validate()
       
   141 {}
       
   142 >>> p.email
       
   143 u'john@example.com'
       
   144 
       
   145 >>> p = Person(**dict(valid_params, email=22))
       
   146 >>> p.validate()
       
   147 {'email': ['Enter a valid e-mail address.']}
       
   148 
       
   149 # Make sure that Date and DateTime return validation errors and don't raise Python errors.
       
   150 >>> Person(name='John Doe', is_child=True, email='abc@def.com').validate()
       
   151 {'favorite_moment': ['This field is required.'], 'birthdate': ['This field is required.']}
       
   152 
       
   153 """}