Formsetsを使う2 - modelformset_factory編
modelformset_factoryとは
modelオブジェクトからformsetsを生成する関数です。
なんとなくModelFormに似ています。
種別・原材料などのマスタ表示、登録、更新などに使用すると」便利です。
ドキュメントのチュートリアルを実行してみる
まずはmodelを作成します。
models.py
from django.db import models
TITLE_CHOICES = (
('MR', 'Mr.'),
('MRS', 'Mrs.'),
('MS', 'Ms.'),
)
class Author(models.Model):
name = models.CharField(max_length = 100)
title = models.CharField(max_length = 3, choices = TITLE_CHOICES)
birth_date = models.DateField(blank = True, null = True)
def __unicode__(self):
self.name
次にmodelformset_factoryを利用してformsetsを生成してみましょう。
>>>from appname.models import Author
>>>from django.forms.models import modelformset_factory
>>>AuthorFormSet = modelformset_factory(Author)
>>>formset = AuthorFormSet()
>>>print(formset)
出力結果は
<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" />
<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>
<tr><th><label for="id_form-0-title">Title:</label></th>
<td><select name="form-0-title" id="id_form-0-title">
<option value="" selected="selected">---------</option>
<option value="MR">Mr.</option>
<option value="MRS">Mrs.</option>
<option value="MS">Ms.</option>
</select></td></tr>
<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>
となります。
データの入ったformsetを生成するために、データを追加します。
>>>Author.objects.create(name = 'Bob', title = 'MR', birth_date = '2009-01-01')
では表示してみましょう。
>>>from appname.models import Author
>>>from django.forms.models import modelformset_factory
>>>AuthorFormSet = modelformset_factory(Author)
>>>formset = AuthorFormSet(queryset = Author.objects.all())
>>>print(formset)
HTMLは
<input type="hidden" name="form-TOTAL_FORMS" value="2" id="id_form-TOTAL_FORMS"/>
<input type="hidden" name="form-INITIAL_FORMS" value="1" id="id_form-INITIAL_FORMS" />
<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="Bob" maxlength="100" /></td></tr>
<tr><th><label for="id_form-0-title">Title:</label></th>
<td><select name="form-0-title" id="id_form-0-title">
<option value="">---------</option>
<option value="MR" selected="selected">Mr.</option>
<option value="MRS">Mrs.</option>
<option value="MS">Ms.</option>
</select></td></tr>
<tr><th><label for="id_form-0-birth_date">Birth date:</label></th>
<td><input type="text" name="form-0-birth_date" value="2009-01-01" id="id_form-0-birth_date" />
<input type="hidden" name="form-0-id" value="1" id="id_form-0-id" /></td></tr>
<tr><th><label for="id_form-1-name">Name:</label></th>
<td><input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /></td></tr>
<tr><th><label for="id_form-1-title">Title:</label></th>
<td><select name="form-1-title" id="id_form-1-title">
<option value="" selected="selected">---------</option>
<option value="MR">Mr.</option>
<option value="MRS">Mrs.</option>
<option value="MS">Ms.</option>
</select></td></tr>
<tr><th><label for="id_form-1-birth_date">Birth date:</label></th>
<td><input type="text" name="form-1-birth_date" id="id_form-1-birth_date" />
<input type="hidden" name="form-1-id" id="id_form-1-id" /></td></tr>
となります。
そしてextraやmax_numなどを指定することも出来ます。
例はextraを指定してみました。
>>>from appname.models import Author
>>>from django.forms.models import modelformset_factory
>>>AuthorFormSet = modelformset_factory(Author, extra = 3)
>>>formset = AuthorFormSet(queryset = Author.objects.all())
>>>print(formset)
<input type="hidden" name="form-TOTAL_FORMS" value="4" id="id_form-TOTAL_FORMS"/>
<input type="hidden" name="form-INITIAL_FORMS" value="1" id="id_form-INITIAL_FORMS" />
<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="Bob" maxlength="100" /></td></tr>
<tr><th><label for="id_form-0-title">Title:</label></th>
<td><select name="form-0-title" id="id_form-0-title">
<option value="">---------</option>
<option value="MR" selected="selected">Mr.</option>
<option value="MRS">Mrs.</option>
<option value="MS">Ms.</option>
</select></td></tr>
<tr><th><label for="id_form-0-birth_date">Birth date:</label></th>
<td><input type="text" name="form-0-birth_date" value="2009-01-01" id="id_form0-birth_date" />
<input type="hidden" name="form-0-id" value="1" id="id_form-0-id" /></td></tr>
<tr><th><label for="id_form-1-name">Name:</label></th>
<td><input id="id_form-1-name" type="text" name="form-1-name" maxlength="100"/></td></tr>
<tr><th><label for="id_form-1-title">Title:</label></th>
<td><select name="form-1-title" id="id_form-1-title">
<option value="" selected="selected">---------</option>
<option value="MR">Mr.</option>
<option value="MRS">Mrs.</option>
<option value="MS">Ms.</option>
</select></td></tr>
<tr><th><label for="id_form-1-birth_date">Birth date:</label></th>
<td><input type="text" name="form-1-birth_date" id="id_form-1-birth_date" />
<input type="hidden" name="form-1-id" id="id_form-1-id" /></td></tr>
<tr><th><label for="id_form-2-name">Name:</label></th>
<td><input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /></td></tr>
<tr><th><label for="id_form-2-title">Title:</label></th>
<td><select name="form-2-title" id="id_form-2-title">
<option value="" selected="selected">---------</option>
<option value="MR">Mr.</option>
<option value="MRS">Mrs.</option>
<option value="MS">Ms.</option>
</select></td></tr>
<tr><th><label for="id_form-2-birth_date">Birth date:</label></th>
<td><input type="text" name="form-2-birth_date" id="id_form-2-birth_date" />
<input type="hidden" name="form-2-id" id="id_form-2-id" /></td></tr>
<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"/></td></tr>
<tr><th><label for="id_form-3-title">Title:</label></th>
<td><select name="form-3-title" id="id_form-3-title">
<option value="" selected="selected">---------</option>
<option value="MR">Mr.</option>
<option value="MRS">Mrs.</option>
<option value="MS">Ms.</option>
</select></td></tr>
<tr><th><label for="id_form-3-birth_date">Birth date:</label></th>
<td><input type="text" name="form-3-birth_date" id="id_form-3-birth_date" />
<input type="hidden" name="form-3-id" id="id_form-3-id" /></td></tr>