Ticket #14655: formset-iterable-docs.diff

File formset-iterable-docs.diff, 6.6 KB (added by kenth, 14 years ago)
  • docs/topics/forms/formsets.txt

    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  
    2323would with a regular form::
    2424
    2525    >>> formset = ArticleFormSet()
    26     >>> for form in formset.forms:
     26    >>> for form in formset:
    2727    ...     print form.as_table()
    2828    <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>
    2929    <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::  
    3535
    3636    >>> ArticleFormSet = formset_factory(ArticleForm, extra=2)
    3737
     38.. versionchanged:: 1.3
     39
     40In prior versions of Django, the formset instance was not iterable. To render
     41the 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
     47Iterating over the ``forms`` list will render the forms in the order
     48they were created. The default formset iterator also renders forms in
     49this order.
     50
    3851Using initial data with a formset
    3952---------------------------------
    4053
    example::  
    5063    ...      'pub_date': datetime.date.today()},
    5164    ... ])
    5265
    53     >>> for form in formset.forms:
     66    >>> for form in formset:
    5467    ...     print form.as_table()
    5568    <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>
    5669    <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::  
    7790
    7891    >>> ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=1)
    7992    >>> formset = ArticleFormset()
    80     >>> for form in formset.forms:
     93    >>> for form in formset:
    8194    ...     print form.as_table()
    8295    <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>
    8396    <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::  
    245258    ...     {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},
    246259    ...     {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},
    247260    ... ])
    248     >>> for form in formset.forms:
     261    >>> for form in formset:
    249262    ...     print form.as_table()
    250263    <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>
    251264    <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::  
    301314    ...     {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},
    302315    ...     {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},
    303316    ... ])
    304     >>> for form in formset.forms:
     317    >>> for form in formset:
    305318    ....    print form.as_table()
    306319    <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" />
    307320    <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::  
    355368
    356369    >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet)
    357370    >>> formset = ArticleFormSet()
    358     >>> for form in formset.forms:
     371    >>> for form in formset:
    359372    ...     print form.as_table()
    360373    <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>
    361374    <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:  
    387400    <form method="post" action="">
    388401        {{ formset.management_form }}
    389402        <table>
    390             {% for form in formset.forms %}
     403            {% for form in formset %}
    391404            {{ form }}
    392405            {% endfor %}
    393406        </table>
  • docs/topics/forms/modelforms.txt

    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``::  
    684684
    685685    >>> AuthorFormSet = modelformset_factory(Author, max_num=4, extra=2)
    686686    >>> formset = AuthorFormSet(queryset=Author.objects.order_by('name'))
    687     >>> for form in formset.forms:
     687    >>> for form in formset:
    688688    ...     print form.as_table()
    689689    <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>
    690690    <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::  
    778778
    779779    <form method="post" action="">
    780780        {{ formset.management_form }}
    781         {% for form in formset.forms %}
     781        {% for form in formset %}
    782782            {{ form }}
    783783        {% endfor %}
    784784    </form>
    Third, you can manually render each field::  
    791791
    792792    <form method="post" action="">
    793793        {{ formset.management_form }}
    794         {% for form in formset.forms %}
     794        {% for form in formset %}
    795795            {% for field in form %}
    796796                {{ field.label_tag }}: {{ field }}
    797797            {% endfor %}
    if you were rendering the ``name`` and ``age`` fields of a model::  
    804804
    805805    <form method="post" action="">
    806806        {{ formset.management_form }}
    807         {% for form in formset.forms %}
     807        {% for form in formset %}
    808808            {{ form.id }}
    809809            <ul>
    810810                <li>{{ form.name }}</li>
Back to Top