Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added some docs on model formsets in views.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8863 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7163fe4d96f7241b9435e91598261f473c098a3a 1 parent bc768e2
Brian Rosner authored September 02, 2008

Showing 1 changed file with 23 additions and 0 deletions. Show diff stats Hide diff stats

  1. 23  docs/topics/forms/modelforms.txt
23  docs/topics/forms/modelforms.txt
@@ -495,6 +495,29 @@ fill the rest with extra forms::
495 495
     <tr><th><label for="id_form-2-name">Name:</label></th><td><input id="id_form-2-name" type="text" name="form-2-name" value="Walt Whitman" maxlength="100" /><input type="hidden" name="form-2-id" value="2" id="id_form-2-id" /></td></tr>
496 496
     <tr><th><label for="id_form-3-name">Name:</label></th><td><input id="id_form-3-name" type="text" name="form-3-name" maxlength="100" /><input type="hidden" name="form-3-id" id="id_form-3-id" /></td></tr>
497 497
 
  498
+Using a model formset in a view
  499
+-------------------------------
  500
+
  501
+Model formsets are very similar to formsets. Lets say we want to present a
  502
+formset to a user to edit ``Author`` model instances::
  503
+
  504
+    def manage_authors(request):
  505
+        AuthorFormSet = modelformset_factory(Author)
  506
+        if request.POST == 'POST':
  507
+            formset = AuthorFormSet(request.POST, request.FILES)
  508
+            if formset.is_valid():
  509
+                formset.save()
  510
+                # do something.
  511
+        else:
  512
+            formset = AuthorFormSet()
  513
+        render_to_response("manage_authors.html", {
  514
+            "formset": formset,
  515
+        })
  516
+
  517
+As you can see the view is not drastically different than how to use a formset
  518
+in a view. The only difference is that we call ``formset.save()`` to save the
  519
+data into the database. This was describe above in :ref:`ref-saving-objects-in-the-formset`.
  520
+
498 521
 Using ``inlineformset_factory``
499 522
 -------------------------------
500 523
 

0 notes on commit 7163fe4

Please sign in to comment.
Something went wrong with that request. Please try again.