| | 1 | """ |
|---|
| | 2 | Tests for the model services for forms. |
|---|
| | 3 | """ |
|---|
| | 4 | |
|---|
| | 5 | from django.db import models |
|---|
| | 6 | from django.newforms.models import form_for_instance, form_for_model |
|---|
| | 7 | from datetime import date |
|---|
| | 8 | |
|---|
| | 9 | class Publication(models.Model): |
|---|
| | 10 | title = models.CharField(maxlength=30) |
|---|
| | 11 | |
|---|
| | 12 | def __str__(self): |
|---|
| | 13 | return self.title |
|---|
| | 14 | |
|---|
| | 15 | class Meta: |
|---|
| | 16 | ordering = ('title',) |
|---|
| | 17 | |
|---|
| | 18 | class Author(models.Model): |
|---|
| | 19 | first_name = models.CharField(maxlength=30) |
|---|
| | 20 | last_name = models.CharField(maxlength=30) |
|---|
| | 21 | |
|---|
| | 22 | def __str__(self): |
|---|
| | 23 | return '%s %s' % (self.first_name, self.last_name) |
|---|
| | 24 | |
|---|
| | 25 | class Meta: |
|---|
| | 26 | ordering = ('last_name','first_name') |
|---|
| | 27 | |
|---|
| | 28 | class Article(models.Model): |
|---|
| | 29 | headline = models.CharField(maxlength=100) |
|---|
| | 30 | publication = models.ForeignKey(Publication) |
|---|
| | 31 | price = models.FloatField(decimal_places=2, max_digits=8) |
|---|
| | 32 | authors = models.ManyToManyField(Author) |
|---|
| | 33 | pub_date = models.DateTimeField() |
|---|
| | 34 | |
|---|
| | 35 | def __str__(self): |
|---|
| | 36 | return self.headline |
|---|
| | 37 | |
|---|
| | 38 | class Meta: |
|---|
| | 39 | ordering = ('headline',) |
|---|
| | 40 | |
|---|
| | 41 | __test__ = {'API_TESTS':""" |
|---|
| | 42 | # Create some Authors. |
|---|
| | 43 | >>> a = Author(first_name='John', last_name='Smith') |
|---|
| | 44 | >>> a.save() |
|---|
| | 45 | |
|---|
| | 46 | >>> a2 = Author(first_name='Peter', last_name='Jones') |
|---|
| | 47 | >>> a2.save() |
|---|
| | 48 | |
|---|
| | 49 | >>> a3 = Author(first_name='Alice', last_name='Fletcher') |
|---|
| | 50 | >>> a3.save() |
|---|
| | 51 | |
|---|
| | 52 | # Create some Publications. |
|---|
| | 53 | >>> p1 = Publication(id=None, title='The Python Journal') |
|---|
| | 54 | >>> p1.save() |
|---|
| | 55 | |
|---|
| | 56 | >>> p2 = Publication(id=None, title='Science News') |
|---|
| | 57 | >>> p2.save() |
|---|
| | 58 | |
|---|
| | 59 | # Create an article |
|---|
| | 60 | >>> art = Article(headline='Django lets you build web apps easily', publication=p1, pub_date=date(2006,02,05), price=2.50) |
|---|
| | 61 | >>> art.save() |
|---|
| | 62 | |
|---|
| | 63 | >>> art.authors = [a, a2] |
|---|
| | 64 | |
|---|
| | 65 | ########################################################### |
|---|
| | 66 | # form_from_model |
|---|
| | 67 | ########################################################### |
|---|
| | 68 | |
|---|
| | 69 | # Create the Form for the Article model |
|---|
| | 70 | >>> ArticleForm = form_for_model(Article) |
|---|
| | 71 | |
|---|
| | 72 | # Instantiate the form with no data |
|---|
| | 73 | >>> f = ArticleForm() |
|---|
| | 74 | >>> print f |
|---|
| | 75 | <tr><th><label for="id_headline">Headline:</label></th><td><input id="id_headline" type="text" name="headline" maxlength="100" /></td></tr> |
|---|
| | 76 | <tr><th><label for="id_publication">Publication:</label></th><td><select name="publication" id="id_publication"> |
|---|
| | 77 | <option value="" selected="selected">---------</option> |
|---|
| | 78 | <option value="2">Science News</option> |
|---|
| | 79 | <option value="1">The Python Journal</option> |
|---|
| | 80 | </select></td></tr> |
|---|
| | 81 | <tr><th><label for="id_price">Price:</label></th><td><input type="text" name="price" id="id_price" /></td></tr> |
|---|
| | 82 | <tr><th><label for="id_pub_date">Pub date:</label></th><td><input type="text" name="pub_date" id="id_pub_date" /></td></tr> |
|---|
| | 83 | <tr><th><label for="id_authors">Authors:</label></th><td><select multiple="multiple" name="authors" id="id_authors"> |
|---|
| | 84 | <option value="3">Alice Fletcher</option> |
|---|
| | 85 | <option value="2">Peter Jones</option> |
|---|
| | 86 | <option value="1">John Smith</option> |
|---|
| | 87 | </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr> |
|---|
| | 88 | |
|---|
| | 89 | # Check that the form is not valid |
|---|
| | 90 | >>> f.is_valid() |
|---|
| | 91 | False |
|---|
| | 92 | |
|---|
| | 93 | # Attempt to save the form. Fail, because form cannot be validated |
|---|
| | 94 | >>> f.save() |
|---|
| | 95 | Traceback (most recent call last): |
|---|
| | 96 | ... |
|---|
| | 97 | AttributeError: 'ArticleForm' object has no attribute 'clean_data' |
|---|
| | 98 | |
|---|
| | 99 | # Instantiate the form with data |
|---|
| | 100 | >>> f = ArticleForm({'headline':'Django cures cancer!', 'publication': 1, 'price':'3.20', 'pub_date':'2006-11-08', 'authors':[1,2]}) |
|---|
| | 101 | >>> print f |
|---|
| | 102 | <tr><th><label for="id_headline">Headline:</label></th><td><input id="id_headline" type="text" name="headline" value="Django cures cancer!" maxlength="100" /></td></tr> |
|---|
| | 103 | <tr><th><label for="id_publication">Publication:</label></th><td><select name="publication" id="id_publication"> |
|---|
| | 104 | <option value="">---------</option> |
|---|
| | 105 | <option value="2">Science News</option> |
|---|
| | 106 | <option value="1" selected="selected">The Python Journal</option> |
|---|
| | 107 | </select></td></tr> |
|---|
| | 108 | <tr><th><label for="id_price">Price:</label></th><td><input type="text" name="price" value="3.20" id="id_price" /></td></tr> |
|---|
| | 109 | <tr><th><label for="id_pub_date">Pub date:</label></th><td><input type="text" name="pub_date" value="2006-11-08" id="id_pub_date" /></td></tr> |
|---|
| | 110 | <tr><th><label for="id_authors">Authors:</label></th><td><select multiple="multiple" name="authors" id="id_authors"> |
|---|
| | 111 | <option value="3">Alice Fletcher</option> |
|---|
| | 112 | <option value="2" selected="selected">Peter Jones</option> |
|---|
| | 113 | <option value="1" selected="selected">John Smith</option> |
|---|
| | 114 | </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr> |
|---|
| | 115 | |
|---|
| | 116 | # Check that the form is valid |
|---|
| | 117 | >>> f.is_valid() |
|---|
| | 118 | True |
|---|
| | 119 | |
|---|
| | 120 | # Form can be saved! |
|---|
| | 121 | >>> f.save() |
|---|
| | 122 | <Article: Django cures cancer!> |
|---|
| | 123 | |
|---|
| | 124 | # There are now 2 articles |
|---|
| | 125 | >>> Article.objects.count() |
|---|
| | 126 | 2 |
|---|
| | 127 | |
|---|
| | 128 | # Instantiate the form with data that is missing some fields |
|---|
| | 129 | >>> f = ArticleForm({'headline':'Django cures cancer!', 'publication': 1, 'authors':[1,2]}) |
|---|
| | 130 | >>> print f |
|---|
| | 131 | <tr><th><label for="id_headline">Headline:</label></th><td><input id="id_headline" type="text" name="headline" value="Django cures cancer!" maxlength="100" /></td></tr> |
|---|
| | 132 | <tr><th><label for="id_publication">Publication:</label></th><td><select name="publication" id="id_publication"> |
|---|
| | 133 | <option value="">---------</option> |
|---|
| | 134 | <option value="2">Science News</option> |
|---|
| | 135 | <option value="1" selected="selected">The Python Journal</option> |
|---|
| | 136 | </select></td></tr> |
|---|
| | 137 | <tr><th><label for="id_price">Price:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="price" id="id_price" /></td></tr> |
|---|
| | 138 | <tr><th><label for="id_pub_date">Pub date:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="pub_date" id="id_pub_date" /></td></tr> |
|---|
| | 139 | <tr><th><label for="id_authors">Authors:</label></th><td><select multiple="multiple" name="authors" id="id_authors"> |
|---|
| | 140 | <option value="3">Alice Fletcher</option> |
|---|
| | 141 | <option value="2" selected="selected">Peter Jones</option> |
|---|
| | 142 | <option value="1" selected="selected">John Smith</option> |
|---|
| | 143 | </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr> |
|---|
| | 144 | |
|---|
| | 145 | # Check that the form is not valid |
|---|
| | 146 | >>> f.is_valid() |
|---|
| | 147 | False |
|---|
| | 148 | |
|---|
| | 149 | # Attempt to save the form. Fail, because form cannot be validated |
|---|
| | 150 | >>> f.save() |
|---|
| | 151 | Traceback (most recent call last): |
|---|
| | 152 | ... |
|---|
| | 153 | ValueError: The Article could not be created because the data didn't validate. |
|---|
| | 154 | |
|---|
| | 155 | ########################################################### |
|---|
| | 156 | # form_from_instance |
|---|
| | 157 | ########################################################### |
|---|
| | 158 | |
|---|
| | 159 | # Create a Form for an instance of Article |
|---|
| | 160 | >>> ArticleInstanceForm = form_for_instance(art) |
|---|
| | 161 | |
|---|
| | 162 | # Instantiate the form with no data |
|---|
| | 163 | >>> f = ArticleInstanceForm() |
|---|
| | 164 | >>> print f |
|---|
| | 165 | <tr><th><label for="id_headline">Headline:</label></th><td><input id="id_headline" type="text" name="headline" value="Django lets you build web apps easily" maxlength="100" /></td></tr> |
|---|
| | 166 | <tr><th><label for="id_publication">Publication:</label></th><td><select name="publication" id="id_publication"> |
|---|
| | 167 | <option value="">---------</option> |
|---|
| | 168 | <option value="2">Science News</option> |
|---|
| | 169 | <option value="1" selected="selected">The Python Journal</option> |
|---|
| | 170 | </select></td></tr> |
|---|
| | 171 | <tr><th><label for="id_price">Price:</label></th><td><input type="text" name="price" value="2.5" id="id_price" /></td></tr> |
|---|
| | 172 | <tr><th><label for="id_pub_date">Pub date:</label></th><td><input type="text" name="pub_date" value="2006-02-05" id="id_pub_date" /></td></tr> |
|---|
| | 173 | <tr><th><label for="id_authors">Authors:</label></th><td><select multiple="multiple" name="authors" id="id_authors"> |
|---|
| | 174 | <option value="3">Alice Fletcher</option> |
|---|
| | 175 | <option value="2" selected="selected">Peter Jones</option> |
|---|
| | 176 | <option value="1" selected="selected">John Smith</option> |
|---|
| | 177 | </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr> |
|---|
| | 178 | |
|---|
| | 179 | # Check that the form is not valid (no data dictionary provided) |
|---|
| | 180 | >>> f.is_valid() |
|---|
| | 181 | False |
|---|
| | 182 | |
|---|
| | 183 | # Attempt to save the form. Fail, because form cannot be validated |
|---|
| | 184 | >>> f.save() |
|---|
| | 185 | Traceback (most recent call last): |
|---|
| | 186 | ... |
|---|
| | 187 | AttributeError: 'ArticleInstanceForm' object has no attribute 'clean_data' |
|---|
| | 188 | |
|---|
| | 189 | # Instantiate the form with data |
|---|
| | 190 | >>> f = ArticleInstanceForm({'headline':'Nasa uses Python', 'publication': 2, 'price':'4.70', 'pub_date':'2007-01-21', 'authors': [3]}) |
|---|
| | 191 | >>> print f |
|---|
| | 192 | <tr><th><label for="id_headline">Headline:</label></th><td><input id="id_headline" type="text" name="headline" value="Nasa uses Python" maxlength="100" /></td></tr> |
|---|
| | 193 | <tr><th><label for="id_publication">Publication:</label></th><td><select name="publication" id="id_publication"> |
|---|
| | 194 | <option value="">---------</option> |
|---|
| | 195 | <option value="2" selected="selected">Science News</option> |
|---|
| | 196 | <option value="1">The Python Journal</option> |
|---|
| | 197 | </select></td></tr> |
|---|
| | 198 | <tr><th><label for="id_price">Price:</label></th><td><input type="text" name="price" value="4.70" id="id_price" /></td></tr> |
|---|
| | 199 | <tr><th><label for="id_pub_date">Pub date:</label></th><td><input type="text" name="pub_date" value="2007-01-21" id="id_pub_date" /></td></tr> |
|---|
| | 200 | <tr><th><label for="id_authors">Authors:</label></th><td><select multiple="multiple" name="authors" id="id_authors"> |
|---|
| | 201 | <option value="3" selected="selected">Alice Fletcher</option> |
|---|
| | 202 | <option value="2">Peter Jones</option> |
|---|
| | 203 | <option value="1">John Smith</option> |
|---|
| | 204 | </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr> |
|---|
| | 205 | |
|---|
| | 206 | # Check that the form is valid |
|---|
| | 207 | >>> f.is_valid() |
|---|
| | 208 | True |
|---|
| | 209 | |
|---|
| | 210 | # Form can be saved! |
|---|
| | 211 | >>> f.save() |
|---|
| | 212 | <Article: Nasa uses Python> |
|---|
| | 213 | |
|---|
| | 214 | # There are still only 2 articles, as we have saved an existing instance |
|---|
| | 215 | >>> Article.objects.count() |
|---|
| | 216 | 2 |
|---|
| | 217 | |
|---|
| | 218 | |
|---|
| | 219 | # Instantiate the form with data that is missing some fields |
|---|
| | 220 | >>> f = ArticleInstanceForm({'headline':'Nasa uses Python', 'publication': 2, 'authors': [3]}) |
|---|
| | 221 | >>> print f |
|---|
| | 222 | <tr><th><label for="id_headline">Headline:</label></th><td><input id="id_headline" type="text" name="headline" value="Nasa uses Python" maxlength="100" /></td></tr> |
|---|
| | 223 | <tr><th><label for="id_publication">Publication:</label></th><td><select name="publication" id="id_publication"> |
|---|
| | 224 | <option value="">---------</option> |
|---|
| | 225 | <option value="2" selected="selected">Science News</option> |
|---|
| | 226 | <option value="1">The Python Journal</option> |
|---|
| | 227 | </select></td></tr> |
|---|
| | 228 | <tr><th><label for="id_price">Price:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="price" id="id_price" /></td></tr> |
|---|
| | 229 | <tr><th><label for="id_pub_date">Pub date:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="pub_date" id="id_pub_date" /></td></tr> |
|---|
| | 230 | <tr><th><label for="id_authors">Authors:</label></th><td><select multiple="multiple" name="authors" id="id_authors"> |
|---|
| | 231 | <option value="3" selected="selected">Alice Fletcher</option> |
|---|
| | 232 | <option value="2">Peter Jones</option> |
|---|
| | 233 | <option value="1">John Smith</option> |
|---|
| | 234 | </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr> |
|---|
| | 235 | |
|---|
| | 236 | # Check that the form is not valid |
|---|
| | 237 | >>> f.is_valid() |
|---|
| | 238 | False |
|---|
| | 239 | |
|---|
| | 240 | # Attempt to save the form. Fail, because form cannot be validated |
|---|
| | 241 | >>> f.save() |
|---|
| | 242 | Traceback (most recent call last): |
|---|
| | 243 | ... |
|---|
| | 244 | ValueError: The Article could not be changed because the data didn't validate. |
|---|
| | 245 | |
|---|
| | 246 | """} |
|---|