public
Description: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Homepage: http://www.djangoproject.com/
Clone URL: git://github.com/brosner/django.git
Initial draft of the model formset docs.
brosner (author)
Mon May 12 15:38:58 -0700 2008
commit  1dafe0165c37342b0a300b0864442447e1e74804
tree    0be876f794682cd8806b4f3a7ed6b14de63c0c9f
parent  bc8baafc4fda02b5b42238bea570350cee786fd6
...
369
370
371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
0
@@ -369,3 +369,40 @@ There are a couple of things to note, however.
0
 
0
 Chances are these notes won't affect you unless you're trying to do something
0
 tricky with subclassing.
0
+
0
+Model Formsets
0
+==============
0
+
0
+Similar to regular formsets there are a couple enhanced formset classes that
0
+provide all the right things to work with your models. Lets reuse the
0
+``Author`` model from above::
0
+
0
+ >>> from django.newforms.models import modelformset_factory
0
+ >>> AuthorFormSet = modelformset_factory(Author)
0
+
0
+This will create a formset that is capable of working with the data associated
0
+to the ``Author`` model. It works just like a regular formset::
0
+
0
+ >>> formset = AuthorFormSet()
0
+ >>> print formset
0
+ <input type="hidden" name="form-TOTAL_FORMS" value="1" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="0" id="id_form-INITIAL_FORMS" />
0
+ <tr><th><label for="id_form-0-name">Name:</label></th><td><input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /></td></tr>
0
+ <tr><th><label for="id_form-0-title">Title:</label></th><td><select name="form-0-title" id="id_form-0-title">
0
+ <option value="" selected="selected">---------</option>
0
+ <option value="MR">Mr.</option>
0
+ <option value="MRS">Mrs.</option>
0
+ <option value="MS">Ms.</option>
0
+ </select></td></tr>
0
+ <tr><th><label for="id_form-0-birth_date">Birth date:</label></th><td><input type="text" name="form-0-birth_date" id="id_form-0-birth_date" /><input type="hidden" name="form-0-id" id="id_form-0-id" /></td></tr>
0
+
0
+One thing to note is that ``modelformset_factory`` uses ``formset_factory``
0
+and by default turns ``can_delete`` to ``True``.
0
+
0
+Changing the queryset
0
+---------------------
0
+
0
+By default when you create a formset from a model the queryset will be all
0
+objects in the model. This is best shown as ``Author.objects.all()``. This is
0
+configurable::
0
+
0
+ >>> formset = AuthorFormSet(queryset=Author.objects.filter(name__startswith='O'))

Comments

    No one has commented yet.