説明
data
またはのformdata
キーワード引数では、 iterable を含むサブフィールドに一致するForm
a を持つ辞書が必要です。その反復可能なニーズのアイテムには、フィールドのリストと一致する属性を持つアイテムがあります。key
FieldList
FieldList
以下の例に従うと、事前に入力されたネストされたフォームがうまく取得されます。
コード
from collections import namedtuple
from wtforms import validators
from wtforms import Form
from wtforms import SelectField
from wtforms import BooleanField
from wtforms import TextField
from wtforms import FieldList
from wtforms import FormField
from webob.multidict import MultiDict
# OP's Code
class FullNameMixIn():
full_name = TextField(
'Full name', [
validators.required(message=u"Full name is required")
])
class GroupRoleForm(Form):
group =BooleanField('Group', default=False)
role = SelectField(
'Role',choices=[
("none", "----------"),
('approver', 'Approver'),
('editor', 'Editor')
])
class AdminEditUserForm(Form, FullNameMixIn):
group_roles = FieldList(FormField(GroupRoleForm))
# create some groups
Group = namedtuple('Group', ['group', 'role'])
g1 = Group('group-1', 'none')
g2 = Group('group-2', 'none')
# drop them in a dictionary
data_in={'group_roles': [g1, g2]}
# Build form
test_form = AdminEditUserForm(data=MultiDict(data_in))
# test print
print test_form.group_roles()
レンダリングされた HTML (切り捨てられた)
<ul id="group_roles">
<li>
<label for="group_roles-0">Group Roles-0</label>
<table id="group_roles-0">
<tr>
<th><label for="gr
oup_roles-0-group">Group</label></th>
<td><input checked id="group_roles-0-group" name="group_roles-0-group" type="checkbox" value="y"><
/td>
</tr>
<tr>
<th><label for="group_roles-0-role">Role</label></th>
<td>
<select id="group_roles-0-role" name="group_roles-0-role">
<option
selected value="none">----------</option>
<option value="approver">Approver</option>
<option value="editor">Editor</option>
</select>
</td
>
</tr>
</table>
</li>
<li>
<label for="group_roles-1">Group Roles-1</label>
<table id="group_roles-1">
<tr>
<th><label for="group_roles-1-gro
up">Group</label></th>
<td><input checked id="group_roles-1-group" name="group_roles-1-group" type="checkbox" value="y"></td>
</tr>
<tr>
<t
h>
<label for="group_roles-1-role">Role</label></th>
<td>
<select id="group_roles-1-role" name="group_roles-1-role">
<option selected value
="none">----------</option>
<option value="approver">Approver</option>
<option value="editor">Editor</option>
</select>
</td>
</tr>
</table>
<
/li>
</ul>
...