说明
在Form中的数据或formdata关键字参数中,您只需要一个与包含可迭代的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(截断)
Group Roles-0
Group < /td>
Role selected value="none">----------
Approver
Editor
>
Group Roles-1
Group h>
Role
="none">----------
Approver
Editor
<
/li>
...