formsets.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. .. _formsets:
  2. Formsets
  3. ========
  4. A formset is a layer of abstraction to working with multiple forms on the same
  5. page. It can be best compared to a data grid. Let's say you have the following
  6. form::
  7. >>> from django import forms
  8. >>> class ArticleForm(forms.Form):
  9. ... title = forms.CharField()
  10. ... pub_date = forms.DateField()
  11. You might want to allow the user to create several articles at once. To create
  12. a formset out of an ``ArticleForm`` you would do::
  13. >>> from django.forms.formsets import formset_factory
  14. >>> ArticleFormSet = formset_factory(ArticleForm)
  15. You now have created a formset named ``ArticleFormSet``. The formset gives you
  16. the ability to iterate over the forms in the formset and display them as you
  17. would with a regular form::
  18. >>> formset = ArticleFormSet()
  19. >>> for form in formset:
  20. ... print(form.as_table())
  21. <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>
  22. <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>
  23. As you can see it only displayed one empty form. The number of empty forms
  24. that is displayed is controlled by the ``extra`` parameter. By default,
  25. ``formset_factory`` defines one extra form; the following example will
  26. display two blank forms::
  27. >>> ArticleFormSet = formset_factory(ArticleForm, extra=2)
  28. .. versionchanged:: 1.3
  29. Prior to Django 1.3, formset instances were not iterable. To render
  30. the formset you iterated over the ``forms`` attribute::
  31. >>> formset = ArticleFormSet()
  32. >>> for form in formset.forms:
  33. ... print(form.as_table())
  34. Iterating over ``formset.forms`` will render the forms in the order
  35. they were created. The default formset iterator also renders the forms
  36. in this order, but you can change this order by providing an alternate
  37. implementation for the :meth:`__iter__()` method.
  38. Formsets can also be indexed into, which returns the corresponding form. If you
  39. override ``__iter__``, you will need to also override ``__getitem__`` to have
  40. matching behavior.
  41. .. _formsets-initial-data:
  42. Using initial data with a formset
  43. ---------------------------------
  44. Initial data is what drives the main usability of a formset. As shown above
  45. you can define the number of extra forms. What this means is that you are
  46. telling the formset how many additional forms to show in addition to the
  47. number of forms it generates from the initial data. Lets take a look at an
  48. example::
  49. >>> ArticleFormSet = formset_factory(ArticleForm, extra=2)
  50. >>> formset = ArticleFormSet(initial=[
  51. ... {'title': u'Django is now open source',
  52. ... 'pub_date': datetime.date.today(),}
  53. ... ])
  54. >>> for form in formset:
  55. ... print(form.as_table())
  56. <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>
  57. <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>
  58. <tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" id="id_form-1-title" /></td></tr>
  59. <tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" id="id_form-1-pub_date" /></td></tr>
  60. <tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr>
  61. <tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr>
  62. There are now a total of three forms showing above. One for the initial data
  63. that was passed in and two extra forms. Also note that we are passing in a
  64. list of dictionaries as the initial data.
  65. .. seealso::
  66. :ref:`Creating formsets from models with model formsets <model-formsets>`.
  67. .. _formsets-max-num:
  68. Limiting the maximum number of forms
  69. ------------------------------------
  70. The ``max_num`` parameter to ``formset_factory`` gives you the ability to
  71. limit the maximum number of empty forms the formset will display::
  72. >>> ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=1)
  73. >>> formset = ArticleFormset()
  74. >>> for form in formset:
  75. ... print(form.as_table())
  76. <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>
  77. <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>
  78. If the value of ``max_num`` is greater than the number of existing
  79. objects, up to ``extra`` additional blank forms will be added to the formset,
  80. so long as the total number of forms does not exceed ``max_num``.
  81. A ``max_num`` value of ``None`` (the default) puts no limit on the number of
  82. forms displayed.
  83. Formset validation
  84. ------------------
  85. Validation with a formset is almost identical to a regular ``Form``. There is
  86. an ``is_valid`` method on the formset to provide a convenient way to validate
  87. all forms in the formset::
  88. >>> ArticleFormSet = formset_factory(ArticleForm)
  89. >>> data = {
  90. ... 'form-TOTAL_FORMS': u'1',
  91. ... 'form-INITIAL_FORMS': u'0',
  92. ... 'form-MAX_NUM_FORMS': u'',
  93. ... }
  94. >>> formset = ArticleFormSet(data)
  95. >>> formset.is_valid()
  96. True
  97. We passed in no data to the formset which is resulting in a valid form. The
  98. formset is smart enough to ignore extra forms that were not changed. If we
  99. provide an invalid article::
  100. >>> data = {
  101. ... 'form-TOTAL_FORMS': u'2',
  102. ... 'form-INITIAL_FORMS': u'0',
  103. ... 'form-MAX_NUM_FORMS': u'',
  104. ... 'form-0-title': u'Test',
  105. ... 'form-0-pub_date': u'1904-06-16',
  106. ... 'form-1-title': u'Test',
  107. ... 'form-1-pub_date': u'', # <-- this date is missing but required
  108. ... }
  109. >>> formset = ArticleFormSet(data)
  110. >>> formset.is_valid()
  111. False
  112. >>> formset.errors
  113. [{}, {'pub_date': [u'This field is required.']}]
  114. As we can see, ``formset.errors`` is a list whose entries correspond to the
  115. forms in the formset. Validation was performed for each of the two forms, and
  116. the expected error message appears for the second item.
  117. .. versionadded:: 1.4
  118. We can also check if form data differs from the initial data (i.e. the form was
  119. sent without any data)::
  120. >>> data = {
  121. ... 'form-TOTAL_FORMS': u'1',
  122. ... 'form-INITIAL_FORMS': u'0',
  123. ... 'form-MAX_NUM_FORMS': u'',
  124. ... 'form-0-title': u'',
  125. ... 'form-0-pub_date': u'',
  126. ... }
  127. >>> formset = ArticleFormSet(data)
  128. >>> formset.has_changed()
  129. False
  130. .. _understanding-the-managementform:
  131. Understanding the ManagementForm
  132. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. You may have noticed the additional data (``form-TOTAL_FORMS``,
  134. ``form-INITIAL_FORMS`` and ``form-MAX_NUM_FORMS``) that was required
  135. in the formset's data above. This data is required for the
  136. ``ManagementForm``. This form is used by the formset to manage the
  137. collection of forms contained in the formset. If you don't provide
  138. this management data, an exception will be raised::
  139. >>> data = {
  140. ... 'form-0-title': u'Test',
  141. ... 'form-0-pub_date': u'',
  142. ... }
  143. >>> formset = ArticleFormSet(data)
  144. Traceback (most recent call last):
  145. ...
  146. django.forms.util.ValidationError: [u'ManagementForm data is missing or has been tampered with']
  147. It is used to keep track of how many form instances are being displayed. If
  148. you are adding new forms via JavaScript, you should increment the count fields
  149. in this form as well.
  150. The management form is available as an attribute of the formset
  151. itself. When rendering a formset in a template, you can include all
  152. the management data by rendering ``{{ my_formset.management_form }}``
  153. (substituting the name of your formset as appropriate).
  154. ``total_form_count`` and ``initial_form_count``
  155. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  156. ``BaseFormSet`` has a couple of methods that are closely related to the
  157. ``ManagementForm``, ``total_form_count`` and ``initial_form_count``.
  158. ``total_form_count`` returns the total number of forms in this formset.
  159. ``initial_form_count`` returns the number of forms in the formset that were
  160. pre-filled, and is also used to determine how many forms are required. You
  161. will probably never need to override either of these methods, so please be
  162. sure you understand what they do before doing so.
  163. ``empty_form``
  164. ~~~~~~~~~~~~~~
  165. ``BaseFormSet`` provides an additional attribute ``empty_form`` which returns
  166. a form instance with a prefix of ``__prefix__`` for easier use in dynamic
  167. forms with JavaScript.
  168. Custom formset validation
  169. ~~~~~~~~~~~~~~~~~~~~~~~~~
  170. A formset has a ``clean`` method similar to the one on a ``Form`` class. This
  171. is where you define your own validation that works at the formset level::
  172. >>> from django.forms.formsets import BaseFormSet
  173. >>> class BaseArticleFormSet(BaseFormSet):
  174. ... def clean(self):
  175. ... """Checks that no two articles have the same title."""
  176. ... if any(self.errors):
  177. ... # Don't bother validating the formset unless each form is valid on its own
  178. ... return
  179. ... titles = []
  180. ... for i in range(0, self.total_form_count()):
  181. ... form = self.forms[i]
  182. ... title = form.cleaned_data['title']
  183. ... if title in titles:
  184. ... raise forms.ValidationError("Articles in a set must have distinct titles.")
  185. ... titles.append(title)
  186. >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet)
  187. >>> data = {
  188. ... 'form-TOTAL_FORMS': u'2',
  189. ... 'form-INITIAL_FORMS': u'0',
  190. ... 'form-MAX_NUM_FORMS': u'',
  191. ... 'form-0-title': u'Test',
  192. ... 'form-0-pub_date': u'1904-06-16',
  193. ... 'form-1-title': u'Test',
  194. ... 'form-1-pub_date': u'1912-06-23',
  195. ... }
  196. >>> formset = ArticleFormSet(data)
  197. >>> formset.is_valid()
  198. False
  199. >>> formset.errors
  200. [{}, {}]
  201. >>> formset.non_form_errors()
  202. [u'Articles in a set must have distinct titles.']
  203. The formset ``clean`` method is called after all the ``Form.clean`` methods
  204. have been called. The errors will be found using the ``non_form_errors()``
  205. method on the formset.
  206. Dealing with ordering and deletion of forms
  207. -------------------------------------------
  208. Common use cases with a formset is dealing with ordering and deletion of the
  209. form instances. This has been dealt with for you. The ``formset_factory``
  210. provides two optional parameters ``can_order`` and ``can_delete`` that will do
  211. the extra work of adding the extra fields and providing simpler ways of
  212. getting to that data.
  213. ``can_order``
  214. ~~~~~~~~~~~~~
  215. Default: ``False``
  216. Lets you create a formset with the ability to order::
  217. >>> ArticleFormSet = formset_factory(ArticleForm, can_order=True)
  218. >>> formset = ArticleFormSet(initial=[
  219. ... {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},
  220. ... {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},
  221. ... ])
  222. >>> for form in formset:
  223. ... print(form.as_table())
  224. <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>
  225. <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>
  226. <tr><th><label for="id_form-0-ORDER">Order:</label></th><td><input type="text" name="form-0-ORDER" value="1" id="id_form-0-ORDER" /></td></tr>
  227. <tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" value="Article #2" id="id_form-1-title" /></td></tr>
  228. <tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" value="2008-05-11" id="id_form-1-pub_date" /></td></tr>
  229. <tr><th><label for="id_form-1-ORDER">Order:</label></th><td><input type="text" name="form-1-ORDER" value="2" id="id_form-1-ORDER" /></td></tr>
  230. <tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr>
  231. <tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr>
  232. <tr><th><label for="id_form-2-ORDER">Order:</label></th><td><input type="text" name="form-2-ORDER" id="id_form-2-ORDER" /></td></tr>
  233. This adds an additional field to each form. This new field is named ``ORDER``
  234. and is an ``forms.IntegerField``. For the forms that came from the initial
  235. data it automatically assigned them a numeric value. Let's look at what will
  236. happen when the user changes these values::
  237. >>> data = {
  238. ... 'form-TOTAL_FORMS': u'3',
  239. ... 'form-INITIAL_FORMS': u'2',
  240. ... 'form-MAX_NUM_FORMS': u'',
  241. ... 'form-0-title': u'Article #1',
  242. ... 'form-0-pub_date': u'2008-05-10',
  243. ... 'form-0-ORDER': u'2',
  244. ... 'form-1-title': u'Article #2',
  245. ... 'form-1-pub_date': u'2008-05-11',
  246. ... 'form-1-ORDER': u'1',
  247. ... 'form-2-title': u'Article #3',
  248. ... 'form-2-pub_date': u'2008-05-01',
  249. ... 'form-2-ORDER': u'0',
  250. ... }
  251. >>> formset = ArticleFormSet(data, initial=[
  252. ... {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},
  253. ... {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},
  254. ... ])
  255. >>> formset.is_valid()
  256. True
  257. >>> for form in formset.ordered_forms:
  258. ... print(form.cleaned_data)
  259. {'pub_date': datetime.date(2008, 5, 1), 'ORDER': 0, 'title': u'Article #3'}
  260. {'pub_date': datetime.date(2008, 5, 11), 'ORDER': 1, 'title': u'Article #2'}
  261. {'pub_date': datetime.date(2008, 5, 10), 'ORDER': 2, 'title': u'Article #1'}
  262. ``can_delete``
  263. ~~~~~~~~~~~~~~
  264. Default: ``False``
  265. Lets you create a formset with the ability to delete::
  266. >>> ArticleFormSet = formset_factory(ArticleForm, can_delete=True)
  267. >>> formset = ArticleFormSet(initial=[
  268. ... {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},
  269. ... {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},
  270. ... ])
  271. >>> for form in formset:
  272. .... print(form.as_table())
  273. <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" />
  274. <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>
  275. <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>
  276. <tr><th><label for="id_form-0-DELETE">Delete:</label></th><td><input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /></td></tr>
  277. <tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" value="Article #2" id="id_form-1-title" /></td></tr>
  278. <tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" value="2008-05-11" id="id_form-1-pub_date" /></td></tr>
  279. <tr><th><label for="id_form-1-DELETE">Delete:</label></th><td><input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE" /></td></tr>
  280. <tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr>
  281. <tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr>
  282. <tr><th><label for="id_form-2-DELETE">Delete:</label></th><td><input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE" /></td></tr>
  283. Similar to ``can_order`` this adds a new field to each form named ``DELETE``
  284. and is a ``forms.BooleanField``. When data comes through marking any of the
  285. delete fields you can access them with ``deleted_forms``::
  286. >>> data = {
  287. ... 'form-TOTAL_FORMS': u'3',
  288. ... 'form-INITIAL_FORMS': u'2',
  289. ... 'form-MAX_NUM_FORMS': u'',
  290. ... 'form-0-title': u'Article #1',
  291. ... 'form-0-pub_date': u'2008-05-10',
  292. ... 'form-0-DELETE': u'on',
  293. ... 'form-1-title': u'Article #2',
  294. ... 'form-1-pub_date': u'2008-05-11',
  295. ... 'form-1-DELETE': u'',
  296. ... 'form-2-title': u'',
  297. ... 'form-2-pub_date': u'',
  298. ... 'form-2-DELETE': u'',
  299. ... }
  300. >>> formset = ArticleFormSet(data, initial=[
  301. ... {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},
  302. ... {'title': u'Article #2', 'pub_date': datetime.date(2008, 5, 11)},
  303. ... ])
  304. >>> [form.cleaned_data for form in formset.deleted_forms]
  305. [{'DELETE': True, 'pub_date': datetime.date(2008, 5, 10), 'title': u'Article #1'}]
  306. Adding additional fields to a formset
  307. -------------------------------------
  308. If you need to add additional fields to the formset this can be easily
  309. accomplished. The formset base class provides an ``add_fields`` method. You
  310. can simply override this method to add your own fields or even redefine the
  311. default fields/attributes of the order and deletion fields::
  312. >>> class BaseArticleFormSet(BaseFormSet):
  313. ... def add_fields(self, form, index):
  314. ... super(BaseArticleFormSet, self).add_fields(form, index)
  315. ... form.fields["my_field"] = forms.CharField()
  316. >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet)
  317. >>> formset = ArticleFormSet()
  318. >>> for form in formset:
  319. ... print(form.as_table())
  320. <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>
  321. <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>
  322. <tr><th><label for="id_form-0-my_field">My field:</label></th><td><input type="text" name="form-0-my_field" id="id_form-0-my_field" /></td></tr>
  323. Using a formset in views and templates
  324. --------------------------------------
  325. Using a formset inside a view is as easy as using a regular ``Form`` class.
  326. The only thing you will want to be aware of is making sure to use the
  327. management form inside the template. Let's look at a sample view:
  328. .. code-block:: python
  329. def manage_articles(request):
  330. ArticleFormSet = formset_factory(ArticleForm)
  331. if request.method == 'POST':
  332. formset = ArticleFormSet(request.POST, request.FILES)
  333. if formset.is_valid():
  334. # do something with the formset.cleaned_data
  335. pass
  336. else:
  337. formset = ArticleFormSet()
  338. return render_to_response('manage_articles.html', {'formset': formset})
  339. The ``manage_articles.html`` template might look like this:
  340. .. code-block:: html+django
  341. <form method="post" action="">
  342. {{ formset.management_form }}
  343. <table>
  344. {% for form in formset %}
  345. {{ form }}
  346. {% endfor %}
  347. </table>
  348. </form>
  349. However the above can be slightly shortcutted and let the formset itself deal
  350. with the management form:
  351. .. code-block:: html+django
  352. <form method="post" action="">
  353. <table>
  354. {{ formset }}
  355. </table>
  356. </form>
  357. The above ends up calling the ``as_table`` method on the formset class.
  358. .. _manually-rendered-can-delete-and-can-order:
  359. Manually rendered ``can_delete`` and ``can_order``
  360. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  361. If you manually render fields in the template, you can render
  362. ``can_delete`` parameter with ``{{ form.DELETE }}``:
  363. .. code-block:: html+django
  364. <form method="post" action="">
  365. {{ formset.management_form }}
  366. {% for form in formset %}
  367. {{ form.id }}
  368. <ul>
  369. <li>{{ form.title }}</li>
  370. {% if formset.can_delete %}
  371. <li>{{ form.DELETE }}</li>
  372. {% endif %}
  373. </ul>
  374. {% endfor %}
  375. </form>
  376. Similarly, if the formset has the ability to order (``can_order=True``), it is possible to render it
  377. with ``{{ form.ORDER }}``.
  378. Using more than one formset in a view
  379. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  380. You are able to use more than one formset in a view if you like. Formsets
  381. borrow much of its behavior from forms. With that said you are able to use
  382. ``prefix`` to prefix formset form field names with a given value to allow
  383. more than one formset to be sent to a view without name clashing. Lets take
  384. a look at how this might be accomplished:
  385. .. code-block:: python
  386. def manage_articles(request):
  387. ArticleFormSet = formset_factory(ArticleForm)
  388. BookFormSet = formset_factory(BookForm)
  389. if request.method == 'POST':
  390. article_formset = ArticleFormSet(request.POST, request.FILES, prefix='articles')
  391. book_formset = BookFormSet(request.POST, request.FILES, prefix='books')
  392. if article_formset.is_valid() and book_formset.is_valid():
  393. # do something with the cleaned_data on the formsets.
  394. pass
  395. else:
  396. article_formset = ArticleFormSet(prefix='articles')
  397. book_formset = BookFormSet(prefix='books')
  398. return render_to_response('manage_articles.html', {
  399. 'article_formset': article_formset,
  400. 'book_formset': book_formset,
  401. })
  402. You would then render the formsets as normal. It is important to point out
  403. that you need to pass ``prefix`` on both the POST and non-POST cases so that
  404. it is rendered and processed correctly.