我们有以下表单,我们正在尝试为每个组创建GroupRoleForms
列表。
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))
如何创建包含预填充的GroupRoleForms
列表的AdminEditUserForm
实例?
目前我们正试图这样做:
form = forms.AdminEditUserForm()
for group in company.groups:
group_role_form = forms.GroupRoleForm()
group_role_form.group.label = group.name
group_role_form.group.name = group.id
form.group_roles.append_entry(group_role_form)
return dict(edit_user_form = form )
解释
在
Form
的data
或formdata
关键字参数中,您只需要一个字典,其中的key
与包含iterable的FieldList
子字段相匹配。iterable中的项依次需要具有与FieldList
的字段列表匹配的项w/属性。如果您按照下面的示例操作,我将得到预填充的嵌套表单。
守则
呈现的HTML(截断)
我不熟悉这些包裹,但我想试试:
你可以这样称呼它:
相关问题 更多 >
编程相关推荐