gpt4 book ai didi

python - 使用 FieldList 和 FormField

转载 作者:太空狗 更新时间:2023-10-29 17:20:11 27 4
gpt4 key购买 nike

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

最佳答案

解释

dataformdata 关键字参数中,Form 您只需要一个带有 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>

...

关于python - 使用 FieldList 和 FormField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12040688/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com