Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 448 lines (405 sloc) 16.319 kb
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
1 """
2 34. Generating HTML forms from models
3
5a01f848 » adrianholovaty
2006-12-30 Updated docstring in model_forms unit test
4 Django provides shortcuts for creating Form objects from a model class and a
5 model instance.
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
6
7 The function django.newforms.form_for_model() takes a model class and returns
8 a Form that is tied to the model. This Form works just like any other Form,
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
9 with one additional method: save(). The save() method creates an instance
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
10 of the model and returns that newly created instance. It saves the instance to
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
11 the database if save(commit=True), which is default. If you pass
12 commit=False, then you'll get the object without committing the changes to the
13 database.
5a01f848 » adrianholovaty
2006-12-30 Updated docstring in model_forms unit test
14
15 The function django.newforms.form_for_instance() takes a model instance and
16 returns a Form that is tied to the instance. This form works just like any
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
17 other Form, with one additional method: save(). The save()
78b89ff1 » adrianholovaty
2007-01-09 Fixed #3232 -- newforms: Added save_instance(), which saves a given b…
18 method updates the model instance. It also takes a commit=True parameter.
19
20 The function django.newforms.save_instance() takes a bound form instance and a
21 model instance and saves the form's clean_data into the instance. It also takes
22 a commit=True parameter.
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
23 """
24
25 from django.db import models
26
27 class Category(models.Model):
28 name = models.CharField(maxlength=20)
35f7e33b » adrianholovaty
2006-12-15 Implemented formfield() for a bunch of database Field classes
29 url = models.CharField('The URL', maxlength=40)
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
30
31 def __str__(self):
32 return self.name
33
6a75c8a5 » adrianholovaty
2006-12-27 newforms: Implemented formfield() for database ForeignKey class and a…
34 class Writer(models.Model):
829e4c70 » adrianholovaty
2007-01-28 newforms: Changed database Field formfield() methods to pass help_tex…
35 name = models.CharField(maxlength=50, help_text='Use both first and last names.')
6a75c8a5 » adrianholovaty
2006-12-27 newforms: Implemented formfield() for database ForeignKey class and a…
36
37 def __str__(self):
38 return self.name
39
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
40 class Article(models.Model):
41 headline = models.CharField(maxlength=50)
b9fdf9ab » adrianholovaty
2006-12-30 newforms: Got form_for_instance() to select initial ManyToManyField v…
42 pub_date = models.DateField()
bdfbcb2c » adrianholovaty
2007-02-20 Fixed #3247 -- newforms form_for_model() and form_for_instance() no l…
43 created = models.DateField(editable=False)
6a75c8a5 » adrianholovaty
2006-12-27 newforms: Implemented formfield() for database ForeignKey class and a…
44 writer = models.ForeignKey(Writer)
963ccd7c » adrianholovaty
2007-01-10 Fixed #3267 -- newforms: Changed database TextField to render as Text…
45 article = models.TextField()
bcb7a31b » adrianholovaty
2006-12-28 newforms: Implemented apply_changes() method for form_for_instance Forms
46 categories = models.ManyToManyField(Category, blank=True)
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
47
bdfbcb2c » adrianholovaty
2007-02-20 Fixed #3247 -- newforms form_for_model() and form_for_instance() no l…
48 def save(self):
49 import datetime
50 if not self.id:
51 self.created = datetime.date.today()
52 return super(Article, self).save()
53
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
54 def __str__(self):
55 return self.headline
56
57 __test__ = {'API_TESTS': """
78b89ff1 » adrianholovaty
2007-01-09 Fixed #3232 -- newforms: Added save_instance(), which saves a given b…
58 >>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField
bcb7a31b » adrianholovaty
2006-12-28 newforms: Implemented apply_changes() method for form_for_instance Forms
59 >>> import datetime
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
60
61 >>> Category.objects.all()
62 []
63
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
64 >>> CategoryForm = form_for_model(Category)
65 >>> f = CategoryForm()
66 >>> print f
35f7e33b » adrianholovaty
2006-12-15 Implemented formfield() for a bunch of database Field classes
67 <tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr>
68 <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr>
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
69 >>> print f.as_ul()
35f7e33b » adrianholovaty
2006-12-15 Implemented formfield() for a bunch of database Field classes
70 <li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" maxlength="20" /></li>
71 <li><label for="id_url">The URL:</label> <input id="id_url" type="text" name="url" maxlength="40" /></li>
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
72 >>> print f['name']
35f7e33b » adrianholovaty
2006-12-15 Implemented formfield() for a bunch of database Field classes
73 <input id="id_name" type="text" name="name" maxlength="20" />
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
74
75 >>> f = CategoryForm(auto_id=False)
76 >>> print f.as_ul()
35f7e33b » adrianholovaty
2006-12-15 Implemented formfield() for a bunch of database Field classes
77 <li>Name: <input type="text" name="name" maxlength="20" /></li>
78 <li>The URL: <input type="text" name="url" maxlength="40" /></li>
5cb093b0 » adrianholovaty
2006-12-15 newforms: Changed form_for_model() to ignore a field if its formfield…
79
80 >>> f = CategoryForm({'name': 'Entertainment', 'url': 'entertainment'})
0421b259 » adrianholovaty
2007-01-08 Fixed #3252 -- Fixed bugs in model_forms unit tests, related to recen…
81 >>> f.is_valid()
82 True
5cb093b0 » adrianholovaty
2006-12-15 newforms: Changed form_for_model() to ignore a field if its formfield…
83 >>> f.clean_data
84 {'url': u'entertainment', 'name': u'Entertainment'}
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
85 >>> obj = f.save()
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
86 >>> obj
87 <Category: Entertainment>
88 >>> Category.objects.all()
89 [<Category: Entertainment>]
90
91 >>> f = CategoryForm({'name': "It's a test", 'url': 'test'})
0421b259 » adrianholovaty
2007-01-08 Fixed #3252 -- Fixed bugs in model_forms unit tests, related to recen…
92 >>> f.is_valid()
93 True
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
94 >>> f.clean_data
95 {'url': u'test', 'name': u"It's a test"}
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
96 >>> obj = f.save()
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
97 >>> obj
98 <Category: It's a test>
99 >>> Category.objects.all()
100 [<Category: Entertainment>, <Category: It's a test>]
101
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
102 If you call save() with commit=False, then it will return an object that
103 hasn't yet been saved to the database. In this case, it's up to you to call
104 save() on the resulting model instance.
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
105 >>> f = CategoryForm({'name': 'Third test', 'url': 'third'})
0421b259 » adrianholovaty
2007-01-08 Fixed #3252 -- Fixed bugs in model_forms unit tests, related to recen…
106 >>> f.is_valid()
107 True
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
108 >>> f.clean_data
109 {'url': u'third', 'name': u'Third test'}
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
110 >>> obj = f.save(commit=False)
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
111 >>> obj
112 <Category: Third test>
113 >>> Category.objects.all()
114 [<Category: Entertainment>, <Category: It's a test>]
115 >>> obj.save()
116 >>> Category.objects.all()
117 [<Category: Entertainment>, <Category: It's a test>, <Category: Third test>]
118
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
119 If you call save() with invalid data, you'll get a ValueError.
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
120 >>> f = CategoryForm({'name': '', 'url': 'foo'})
121 >>> f.errors
122 {'name': [u'This field is required.']}
123 >>> f.clean_data
0421b259 » adrianholovaty
2007-01-08 Fixed #3252 -- Fixed bugs in model_forms unit tests, related to recen…
124 Traceback (most recent call last):
125 ...
126 AttributeError: 'CategoryForm' object has no attribute 'clean_data'
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
127 >>> f.save()
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
128 Traceback (most recent call last):
129 ...
130 ValueError: The Category could not be created because the data didn't validate.
131 >>> f = CategoryForm({'name': '', 'url': 'foo'})
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
132 >>> f.save()
4f5170bf » adrianholovaty
2006-12-15 newforms: The Form classes created by form_for_model() now have a cre…
133 Traceback (most recent call last):
134 ...
135 ValueError: The Category could not be created because the data didn't validate.
136
6a75c8a5 » adrianholovaty
2006-12-27 newforms: Implemented formfield() for database ForeignKey class and a…
137 Create a couple of Writers.
138 >>> w = Writer(name='Mike Royko')
139 >>> w.save()
140 >>> w = Writer(name='Bob Woodward')
141 >>> w.save()
142
143 ManyToManyFields are represented by a MultipleChoiceField, and ForeignKeys are
144 represented by a ChoiceField.
d8532782 » adrianholovaty
2006-12-27 newforms: Implemented formfield() for database ManyToManyField class …
145 >>> ArticleForm = form_for_model(Article)
146 >>> f = ArticleForm(auto_id=False)
147 >>> print f
148 <tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr>
149 <tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr>
6a75c8a5 » adrianholovaty
2006-12-27 newforms: Implemented formfield() for database ForeignKey class and a…
150 <tr><th>Writer:</th><td><select name="writer">
151 <option value="" selected="selected">---------</option>
152 <option value="1">Mike Royko</option>
153 <option value="2">Bob Woodward</option>
154 </select></td></tr>
963ccd7c » adrianholovaty
2007-01-10 Fixed #3267 -- newforms: Changed database TextField to render as Text…
155 <tr><th>Article:</th><td><textarea name="article"></textarea></td></tr>
d8532782 » adrianholovaty
2006-12-27 newforms: Implemented formfield() for database ManyToManyField class …
156 <tr><th>Categories:</th><td><select multiple="multiple" name="categories">
157 <option value="1">Entertainment</option>
158 <option value="2">It&#39;s a test</option>
159 <option value="3">Third test</option>
829e4c70 » adrianholovaty
2007-01-28 newforms: Changed database Field formfield() methods to pass help_tex…
160 </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr>
d8532782 » adrianholovaty
2006-12-27 newforms: Implemented formfield() for database ManyToManyField class …
161
a0ef6f69 » adrianholovaty
2006-12-17 newforms: Added optional 'form' parameter to form_for_model
162 You can pass a custom Form class to form_for_model. Make sure it's a
163 subclass of BaseForm, not Form.
164 >>> class CustomForm(BaseForm):
165 ... def say_hello(self):
166 ... print 'hello'
167 >>> CategoryForm = form_for_model(Category, form=CustomForm)
168 >>> f = CategoryForm()
169 >>> f.say_hello()
170 hello
71ce11f6 » adrianholovaty
2006-12-28 newforms: Implemented form_for_instance(). The resulting Form class d…
171
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
172 Use form_for_instance to create a Form from a model instance. The difference
173 between this Form and one created via form_for_model is that the object's
174 current values are inserted as 'initial' data in each Field.
71ce11f6 » adrianholovaty
2006-12-28 newforms: Implemented form_for_instance(). The resulting Form class d…
175 >>> w = Writer.objects.get(name='Mike Royko')
176 >>> RoykoForm = form_for_instance(w)
177 >>> f = RoykoForm(auto_id=False)
178 >>> print f
829e4c70 » adrianholovaty
2007-01-28 newforms: Changed database Field formfield() methods to pass help_tex…
179 <tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr>
bcb7a31b » adrianholovaty
2006-12-28 newforms: Implemented apply_changes() method for form_for_instance Forms
180
963ccd7c » adrianholovaty
2007-01-10 Fixed #3267 -- newforms: Changed database TextField to render as Text…
181 >>> art = Article(headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.')
bcb7a31b » adrianholovaty
2006-12-28 newforms: Implemented apply_changes() method for form_for_instance Forms
182 >>> art.save()
183 >>> art.id
184 1
185 >>> TestArticleForm = form_for_instance(art)
186 >>> f = TestArticleForm(auto_id=False)
187 >>> print f.as_ul()
188 <li>Headline: <input type="text" name="headline" value="Test article" maxlength="50" /></li>
189 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
190 <li>Writer: <select name="writer">
191 <option value="">---------</option>
192 <option value="1" selected="selected">Mike Royko</option>
193 <option value="2">Bob Woodward</option>
194 </select></li>
963ccd7c » adrianholovaty
2007-01-10 Fixed #3267 -- newforms: Changed database TextField to render as Text…
195 <li>Article: <textarea name="article">Hello.</textarea></li>
bcb7a31b » adrianholovaty
2006-12-28 newforms: Implemented apply_changes() method for form_for_instance Forms
196 <li>Categories: <select multiple="multiple" name="categories">
197 <option value="1">Entertainment</option>
198 <option value="2">It&#39;s a test</option>
199 <option value="3">Third test</option>
829e4c70 » adrianholovaty
2007-01-28 newforms: Changed database Field formfield() methods to pass help_tex…
200 </select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
963ccd7c » adrianholovaty
2007-01-10 Fixed #3267 -- newforms: Changed database TextField to render as Text…
201 >>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 'writer': u'1', 'article': 'Hello.'})
bcb7a31b » adrianholovaty
2006-12-28 newforms: Implemented apply_changes() method for form_for_instance Forms
202 >>> f.is_valid()
203 True
d08112a6 » adrianholovaty
2007-01-09 newforms: Changed model auto-Form generation so that create() and app…
204 >>> new_art = f.save()
bcb7a31b » adrianholovaty
2006-12-28 newforms: Implemented apply_changes() method for form_for_instance Forms
205 >>> new_art.id
206 1
207 >>> new_art = Article.objects.get(id=1)
208 >>> new_art.headline
209 'New headline'
b9fdf9ab » adrianholovaty
2006-12-30 newforms: Got form_for_instance() to select initial ManyToManyField v…
210
211 Add some categories and test the many-to-many form output.
212 >>> new_art.categories.all()
213 []
214 >>> new_art.categories.add(Category.objects.get(name='Entertainment'))
215 >>> new_art.categories.all()
216 [<Category: Entertainment>]
217 >>> TestArticleForm = form_for_instance(new_art)
218 >>> f = TestArticleForm(auto_id=False)
219 >>> print f.as_ul()
220 <li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li>
221 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
222 <li>Writer: <select name="writer">
223 <option value="">---------</option>
224 <option value="1" selected="selected">Mike Royko</option>
225 <option value="2">Bob Woodward</option>
226 </select></li>
963ccd7c » adrianholovaty
2007-01-10 Fixed #3267 -- newforms: Changed database TextField to render as Text…
227 <li>Article: <textarea name="article">Hello.</textarea></li>
b9fdf9ab » adrianholovaty
2006-12-30 newforms: Got form_for_instance() to select initial ManyToManyField v…
228 <li>Categories: <select multiple="multiple" name="categories">
229 <option value="1" selected="selected">Entertainment</option>
230 <option value="2">It&#39;s a test</option>
231 <option value="3">Third test</option>
829e4c70 » adrianholovaty
2007-01-28 newforms: Changed database Field formfield() methods to pass help_tex…
232 </select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
b9fdf9ab » adrianholovaty
2006-12-30 newforms: Got form_for_instance() to select initial ManyToManyField v…
233
83768bf0 » adrianholovaty
2007-01-28 Fixed #3263 -- newforms form_for_model() and form_for_instance() now …
234 >>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04',
235 ... 'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']})
236 >>> new_art = f.save()
237 >>> new_art.id
238 1
239 >>> new_art = Article.objects.get(id=1)
240 >>> new_art.categories.all()
241 [<Category: Entertainment>, <Category: It's a test>]
242
243 Now, submit form data with no categories. This deletes the existing categories.
244 >>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04',
245 ... 'writer': u'1', 'article': u'Hello.'})
246 >>> new_art = f.save()
247 >>> new_art.id
248 1
249 >>> new_art = Article.objects.get(id=1)
250 >>> new_art.categories.all()
251 []
252
253 Create a new article, with categories, via the form.
254 >>> ArticleForm = form_for_model(Article)
255 >>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01',
256 ... 'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']})
257 >>> new_art = f.save()
258 >>> new_art.id
259 2
260 >>> new_art = Article.objects.get(id=2)
261 >>> new_art.categories.all()
262 [<Category: Entertainment>, <Category: It's a test>]
263
264 Create a new article, with no categories, via the form.
265 >>> ArticleForm = form_for_model(Article)
266 >>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01',
267 ... 'writer': u'1', 'article': u'Test.'})
268 >>> new_art = f.save()
269 >>> new_art.id
270 3
271 >>> new_art = Article.objects.get(id=3)
272 >>> new_art.categories.all()
273 []
274
78b89ff1 » adrianholovaty
2007-01-09 Fixed #3232 -- newforms: Added save_instance(), which saves a given b…
275 Here, we define a custom Form. Because it happens to have the same fields as
276 the Category model, we can use save_instance() to apply its changes to an
277 existing Category instance.
278 >>> class ShortCategory(Form):
279 ... name = CharField(max_length=5)
280 ... url = CharField(max_length=3)
281 >>> cat = Category.objects.get(name='Third test')
282 >>> cat
283 <Category: Third test>
284 >>> cat.id
285 3
286 >>> sc = ShortCategory({'name': 'Third', 'url': '3rd'})
287 >>> save_instance(sc, cat)
288 <Category: Third>
289 >>> Category.objects.get(id=3)
290 <Category: Third>
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
291
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
292 Here, we demonstrate that choices for a ForeignKey ChoiceField are determined
293 at runtime, based on the data in the database when the form is displayed, not
294 the data in the database when the form is instantiated.
295 >>> ArticleForm = form_for_model(Article)
296 >>> f = ArticleForm(auto_id=False)
297 >>> print f.as_ul()
298 <li>Headline: <input type="text" name="headline" maxlength="50" /></li>
299 <li>Pub date: <input type="text" name="pub_date" /></li>
300 <li>Writer: <select name="writer">
301 <option value="" selected="selected">---------</option>
302 <option value="1">Mike Royko</option>
303 <option value="2">Bob Woodward</option>
304 </select></li>
305 <li>Article: <textarea name="article"></textarea></li>
306 <li>Categories: <select multiple="multiple" name="categories">
307 <option value="1">Entertainment</option>
308 <option value="2">It&#39;s a test</option>
309 <option value="3">Third</option>
310 </select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
311 >>> Category.objects.create(name='Fourth', url='4th')
312 <Category: Fourth>
313 >>> Writer.objects.create(name='Carl Bernstein')
314 <Writer: Carl Bernstein>
315 >>> print f.as_ul()
316 <li>Headline: <input type="text" name="headline" maxlength="50" /></li>
317 <li>Pub date: <input type="text" name="pub_date" /></li>
318 <li>Writer: <select name="writer">
319 <option value="" selected="selected">---------</option>
320 <option value="1">Mike Royko</option>
321 <option value="2">Bob Woodward</option>
322 <option value="3">Carl Bernstein</option>
323 </select></li>
324 <li>Article: <textarea name="article"></textarea></li>
325 <li>Categories: <select multiple="multiple" name="categories">
326 <option value="1">Entertainment</option>
327 <option value="2">It&#39;s a test</option>
328 <option value="3">Third</option>
329 <option value="4">Fourth</option>
330 </select> Hold down "Control", or "Command" on a Mac, to select more than one.</li>
331
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
332 # ModelChoiceField ############################################################
333
334 >>> from django.newforms import ModelChoiceField, ModelMultipleChoiceField
335
336 >>> f = ModelChoiceField(Category.objects.all())
337 >>> f.clean('')
338 Traceback (most recent call last):
339 ...
340 ValidationError: [u'This field is required.']
341 >>> f.clean(None)
342 Traceback (most recent call last):
343 ...
344 ValidationError: [u'This field is required.']
345 >>> f.clean(0)
346 Traceback (most recent call last):
347 ...
348 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
349 >>> f.clean(3)
350 <Category: Third>
351 >>> f.clean(2)
352 <Category: It's a test>
353
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
354 # Add a Category object *after* the ModelChoiceField has already been
355 # instantiated. This proves clean() checks the database during clean() rather
356 # than caching it at time of instantiation.
357 >>> Category.objects.create(name='Fifth', url='5th')
358 <Category: Fifth>
359 >>> f.clean(5)
360 <Category: Fifth>
361
362 # Delete a Category object *after* the ModelChoiceField has already been
363 # instantiated. This proves clean() checks the database during clean() rather
364 # than caching it at time of instantiation.
365 >>> Category.objects.get(url='5th').delete()
366 >>> f.clean(5)
367 Traceback (most recent call last):
368 ...
369 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
370
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
371 >>> f = ModelChoiceField(Category.objects.filter(pk=1), required=False)
372 >>> print f.clean('')
373 None
374 >>> f.clean('')
375 >>> f.clean('1')
376 <Category: Entertainment>
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
377 >>> f.clean('100')
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
378 Traceback (most recent call last):
379 ...
380 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
381
382 # ModelMultipleChoiceField ####################################################
383
384 >>> f = ModelMultipleChoiceField(Category.objects.all())
385 >>> f.clean(None)
386 Traceback (most recent call last):
387 ...
388 ValidationError: [u'This field is required.']
389 >>> f.clean([])
390 Traceback (most recent call last):
391 ...
392 ValidationError: [u'This field is required.']
393 >>> f.clean([1])
394 [<Category: Entertainment>]
395 >>> f.clean([2])
396 [<Category: It's a test>]
397 >>> f.clean(['1'])
398 [<Category: Entertainment>]
399 >>> f.clean(['1', '2'])
400 [<Category: Entertainment>, <Category: It's a test>]
401 >>> f.clean([1, '2'])
402 [<Category: Entertainment>, <Category: It's a test>]
403 >>> f.clean((1, '2'))
404 [<Category: Entertainment>, <Category: It's a test>]
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
405 >>> f.clean(['100'])
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
406 Traceback (most recent call last):
407 ...
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
408 ValidationError: [u'Select a valid choice. 100 is not one of the available choices.']
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
409 >>> f.clean('hello')
410 Traceback (most recent call last):
411 ...
412 ValidationError: [u'Enter a list of values.']
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
413
414 # Add a Category object *after* the ModelChoiceField has already been
415 # instantiated. This proves clean() checks the database during clean() rather
416 # than caching it at time of instantiation.
417 >>> Category.objects.create(id=6, name='Sixth', url='6th')
418 <Category: Sixth>
419 >>> f.clean([6])
420 [<Category: Sixth>]
421
422 # Delete a Category object *after* the ModelChoiceField has already been
423 # instantiated. This proves clean() checks the database during clean() rather
424 # than caching it at time of instantiation.
425 >>> Category.objects.get(url='6th').delete()
426 >>> f.clean([6])
427 Traceback (most recent call last):
428 ...
429 ValidationError: [u'Select a valid choice. 6 is not one of the available choices.']
430
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
431 >>> f = ModelMultipleChoiceField(Category.objects.all(), required=False)
432 >>> f.clean([])
433 []
434 >>> f.clean(())
435 []
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
436 >>> f.clean(['10'])
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
437 Traceback (most recent call last):
438 ...
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
439 ValidationError: [u'Select a valid choice. 10 is not one of the available choices.']
440 >>> f.clean(['3', '10'])
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
441 Traceback (most recent call last):
442 ...
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
443 ValidationError: [u'Select a valid choice. 10 is not one of the available choices.']
444 >>> f.clean(['1', '10'])
e56934b9 » adrianholovaty
2007-02-20 Fixed #3257 -- Added newforms ModelChoiceField and ModelMultipleChoic…
445 Traceback (most recent call last):
446 ...
ee96c7eb » adrianholovaty
2007-02-21 Fixed #3534 -- newforms ModelChoiceField and ModelMultipleChoiceField…
447 ValidationError: [u'Select a valid choice. 10 is not one of the available choices.']
6001974e » adrianholovaty
2006-12-15 newforms: Added initial implementation of form_for_model and form_for…
448 """}
Something went wrong with that request. Please try again.