diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt
index e7b09dc..506fe4c 100644
a
|
b
|
the ability to iterate over the forms in the formset and display them as you
|
23 | 23 | would with a regular form:: |
24 | 24 | |
25 | 25 | >>> formset = ArticleFormSet() |
26 | | >>> for form in formset.forms: |
| 26 | >>> for form in formset: |
27 | 27 | ... print form.as_table() |
28 | 28 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr> |
29 | 29 | <tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /></td></tr> |
… |
… |
display two blank forms::
|
35 | 35 | |
36 | 36 | >>> ArticleFormSet = formset_factory(ArticleForm, extra=2) |
37 | 37 | |
| 38 | .. versionchanged:: 1.3 |
| 39 | |
| 40 | In prior versions of Django, the formset instance was not iterable. To render |
| 41 | the formset you iterated over the ``forms`` list like this:: |
| 42 | |
| 43 | >>> formset = ArticleFormSet() |
| 44 | >>> for form in formset.forms: |
| 45 | ... print form.as_table() |
| 46 | |
| 47 | Iterating over the ``forms`` list will render the forms in the order |
| 48 | they were created. The default formset iterator also renders forms in |
| 49 | this order. |
| 50 | |
38 | 51 | Using initial data with a formset |
39 | 52 | --------------------------------- |
40 | 53 | |
… |
… |
example::
|
50 | 63 | ... 'pub_date': datetime.date.today()}, |
51 | 64 | ... ]) |
52 | 65 | |
53 | | >>> for form in formset.forms: |
| 66 | >>> for form in formset: |
54 | 67 | ... print form.as_table() |
55 | 68 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Django is now open source" id="id_form-0-title" /></td></tr> |
56 | 69 | <tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" value="2008-05-12" id="id_form-0-pub_date" /></td></tr> |
… |
… |
limit the maximum number of empty forms the formset will display::
|
77 | 90 | |
78 | 91 | >>> ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=1) |
79 | 92 | >>> formset = ArticleFormset() |
80 | | >>> for form in formset.forms: |
| 93 | >>> for form in formset: |
81 | 94 | ... print form.as_table() |
82 | 95 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr> |
83 | 96 | <tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /></td></tr> |
… |
… |
Lets create a formset with the ability to order::
|
245 | 258 | ... {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)}, |
246 | 259 | ... {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)}, |
247 | 260 | ... ]) |
248 | | >>> for form in formset.forms: |
| 261 | >>> for form in formset: |
249 | 262 | ... print form.as_table() |
250 | 263 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Article #1" id="id_form-0-title" /></td></tr> |
251 | 264 | <tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" value="2008-05-10" id="id_form-0-pub_date" /></td></tr> |
… |
… |
Lets create a formset with the ability to delete::
|
301 | 314 | ... {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)}, |
302 | 315 | ... {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)}, |
303 | 316 | ... ]) |
304 | | >>> for form in formset.forms: |
| 317 | >>> for form in formset: |
305 | 318 | .... print form.as_table() |
306 | 319 | <input type="hidden" name="form-TOTAL_FORMS" value="3" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="2" id="id_form-INITIAL_FORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS" /> |
307 | 320 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Article #1" id="id_form-0-title" /></td></tr> |
… |
… |
default fields/attributes of the order and deletion fields::
|
355 | 368 | |
356 | 369 | >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet) |
357 | 370 | >>> formset = ArticleFormSet() |
358 | | >>> for form in formset.forms: |
| 371 | >>> for form in formset: |
359 | 372 | ... print form.as_table() |
360 | 373 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr> |
361 | 374 | <tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" id="id_form-0-pub_date" /></td></tr> |
… |
… |
The ``manage_articles.html`` template might look like this:
|
387 | 400 | <form method="post" action=""> |
388 | 401 | {{ formset.management_form }} |
389 | 402 | <table> |
390 | | {% for form in formset.forms %} |
| 403 | {% for form in formset %} |
391 | 404 | {{ form }} |
392 | 405 | {% endfor %} |
393 | 406 | </table> |
diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt
index 65bc0ea..274adba 100644
a
|
b
|
so long as the total number of forms does not exceed ``max_num``::
|
684 | 684 | |
685 | 685 | >>> AuthorFormSet = modelformset_factory(Author, max_num=4, extra=2) |
686 | 686 | >>> formset = AuthorFormSet(queryset=Author.objects.order_by('name')) |
687 | | >>> for form in formset.forms: |
| 687 | >>> for form in formset: |
688 | 688 | ... print form.as_table() |
689 | 689 | <tr><th><label for="id_form-0-name">Name:</label></th><td><input id="id_form-0-name" type="text" name="form-0-name" value="Charles Baudelaire" maxlength="100" /><input type="hidden" name="form-0-id" value="1" id="id_form-0-id" /></td></tr> |
690 | 690 | <tr><th><label for="id_form-1-name">Name:</label></th><td><input id="id_form-1-name" type="text" name="form-1-name" value="Paul Verlaine" maxlength="100" /><input type="hidden" name="form-1-id" value="3" id="id_form-1-id" /></td></tr> |
… |
… |
itself::
|
778 | 778 | |
779 | 779 | <form method="post" action=""> |
780 | 780 | {{ formset.management_form }} |
781 | | {% for form in formset.forms %} |
| 781 | {% for form in formset %} |
782 | 782 | {{ form }} |
783 | 783 | {% endfor %} |
784 | 784 | </form> |
… |
… |
Third, you can manually render each field::
|
791 | 791 | |
792 | 792 | <form method="post" action=""> |
793 | 793 | {{ formset.management_form }} |
794 | | {% for form in formset.forms %} |
| 794 | {% for form in formset %} |
795 | 795 | {% for field in form %} |
796 | 796 | {{ field.label_tag }}: {{ field }} |
797 | 797 | {% endfor %} |
… |
… |
if you were rendering the ``name`` and ``age`` fields of a model::
|
804 | 804 | |
805 | 805 | <form method="post" action=""> |
806 | 806 | {{ formset.management_form }} |
807 | | {% for form in formset.forms %} |
| 807 | {% for form in formset %} |
808 | 808 | {{ form.id }} |
809 | 809 | <ul> |
810 | 810 | <li>{{ form.name }}</li> |