Python 使用FieldList和FormField

Python 使用FieldList和FormField,python,wtforms,Python,Wtforms,我们有以下表单,我们正在尝试为每个组创建GroupRoleForms列表 class FullNameMixIn(): full_name = TextField( 'Full name', [ validators.required(message=u"Full name is required") ]) class GroupRoleForm(Form): group =BooleanField('Group', de

我们有以下表单,我们正在尝试为每个组创建
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 )

我不熟悉这些软件包,但我想尝试一下:

class AdminEditUserForm(Form, FullNameMixIn):
    def __init__(self, groups):
        super(AdminEditUserForm, self).__init__()
        self.group_roles = FieldList(FormField(GroupRoleForm))
        for group in groups:
            self.group.label = group.name
            self.group.name = group.id
            self.group_roles.append_entry(self.group)  
            # If this doesn't create a copy of the GroupRoleForm 
            # superclass in group_roles, then you need a method to do it
            self.__clear_group()

    def __clear_group(self):
        # copy GroupRoleForm object, if needed
        # delete GroupRoleForm object
        ...
那么你可以这样称呼它:

form = forms.AdminEditUserForm(company.groups)
解释 在
表单
数据
表单数据
关键字参数中,您只需要一个带有
的字典,该键与包含iterable的
字段列表
子字段相匹配。iterable中的项依次具有与
字段列表
的字段列表匹配的项和属性

如果您遵循下面的示例,我会得到预填充的嵌套表单

代码 呈现的HTML(被截断)
  • 组角色-0 团体 < /td> 角色 ---------- 批准人 编辑
  • 组角色-1 团体 角色 ---------- 批准人 编辑 < /李>
...
你有没有抽出时间来解决这个问题?如果您这样做了,请发布您的解决方案您想动态追加字段是吗?您知道append_条目不会获取formdata。很简单,通过添加一个init函数,就可以做到这一点。我认为您的动态表单组合可能有点过头了。只需将数据绑定到顶级表单对象,让WTForms完成工作,就可以达到预期效果。我在这里创建了一个完整的工作示例:有趣的是,WTForms说,由于html限制,您不能在字段列表中使用布尔字段,没有检查您的示例,但要小心。mreh仍然适用于所有其他字段类型<代码>SubmitField由于明显的原因不起作用
BooleanField
在编码表单时必须失败,但它可能非常微妙(取最后一个复选框的值或其他内容),最近我找到了这个答案。基于对我有效的方式,我认为关于boolean&submit字段的FieldList限制确实直接适用于这些字段,但不适用于FormField中包含的子表单。另外,
格式(data=…)
中的数据参数不需要是一个MultiDict,标准dict就可以了。@VPfB-这是真的。您可以只使用
FieldList(FormField(MyFormWithBooleanFields))
就可以了。
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()
<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>

...