Skip to content

Commit

Permalink
Fixed #23307 -- Edited max_num section of formsets doccumentation
Browse files Browse the repository at this point in the history
Clarified the way max_num limits the output of formsets.
Added two new examples to show the different scenarios where
max_num affects the output.
  • Loading branch information
Octowl committed Sep 6, 2014
1 parent 14c8456 commit 08959f8
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions docs/topics/forms/formsets.txt
Expand Up @@ -91,7 +91,7 @@ Limiting the maximum number of forms
------------------------------------

The ``max_num`` parameter to :func:`~django.forms.formsets.formset_factory`
gives you the ability to limit the maximum number of empty forms the formset
gives you the ability to limit the number of forms the formset
will display::

>>> from django.forms.formsets import formset_factory
Expand All @@ -103,15 +103,44 @@ will display::
<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>
<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>

If the value of ``max_num`` is greater than the number of existing
objects, up to ``extra`` additional blank forms will be added to the formset,
so long as the total number of forms does not exceed ``max_num``.
If the value of ``max_num`` is greater than the number of existing forms in the
initial data, up to ``extra`` additional blank forms will be added to the formset,
so long as the total number of forms does not exceed ``max_num``::

A ``max_num`` value of ``None`` (the default) puts a high limit on the number
of forms displayed (1000). In practice this is equivalent to no limit.
>>> import datetime
>>> from django.forms.formsets import formset_factory
>>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=2)
>>> formset = ArticleFormSet(initial=[
... {'title': 'Django is now open source',
... 'pub_date': datetime.date.today(),}
... ])
<tr><th><label for="id_form-0-title">Title:</label></th><td><input id="id_form-0-title" name="form-0-title" type="text" value="Django is now open source" /></td></tr>
<tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input id="id_form-0-pub_date" name="form-0-pub_date" type="text" value="2014-09-06" /></td></tr>
<tr><th><label for="id_form-1-title">Title:</label></th><td><input id="id_form-1-title" name="form-1-title" type="text" /></td></tr>
<tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input id="id_form-1-pub_date" name="form-1-pub_date" type="text" /></td></tr>

If the number of forms in the initial data exceeds ``max_num``, all initial
data forms will be displayed regardless. (No extra forms will be displayed.)
data forms will be displayed regardless of the value of ``max_num``.
(No extra forms will be displayed.)::

>>> import datetime
>>> from django.forms.formsets import formset_factory
>>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm, extra=3, max_num=1)
>>> formset = ArticleFormSet(initial=[
... {'title': 'Django is now open source',
... 'pub_date': datetime.date.today(),},
... {'title': 'Django 1.7 has been released',
... 'pub_date': datetime.date.today(),}
... ])
<tr><th><label for="id_form-0-title">Title:</label></th><td><input id="id_form-0-title" name="form-0-title" type="text" value="Django is now open source" /></td></tr>
<tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input id="id_form-0-pub_date" name="form-0-pub_date" type="text" value="2014-09-06" /></td></tr>
<tr><th><label for="id_form-1-title">Title:</label></th><td><input id="id_form-1-title" name="form-1-title" type="text" value="Django 1.7 has been released" /></td></tr>
<tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input id="id_form-1-pub_date" name="form-1-pub_date" type="text" value="2014-09-06" /></td></tr>

A ``max_num`` value of ``None`` (the default) puts a high limit on the number
of forms displayed (1000). In practice this is equivalent to no limit.

By default, ``max_num`` only affects how many forms are displayed and does not
affect validation. If ``validate_max=True`` is passed to the
Expand Down

0 comments on commit 08959f8

Please sign in to comment.