gpt4 book ai didi

forms - 如何在 validate_on_submit() block 之后填充 WTForms FieldList?

转载 作者:行者123 更新时间:2023-12-02 04:25:28 25 4
gpt4 key购买 nike

确实缺乏有关如何使用 WTForms 的 FieldList 的文档。因此,多亏了互联网,我才能够将以下内容组合在一起:

表格:

class BranchForm(Form):
name = StringField('Name', validators = [Required()])
equipment = FieldList(SelectField('Equipment', validators=[Required()], coerce=int,
choices = [(x.id, x.name) for x in Equipment.query.all()]))
mod = FieldList(StringField('Method of Delivery', validators = [Optional()]))

查看:

def edit_branch(id):
branch = Branch.query.filter_by(id=id).first()

#populate data_in to be used by BranchForm
data_in = []
for eq_obj in branch.equipment_assoc:
data_in.append(('equipment', eq_obj.equipment.id))
data_in.append(('mod', eq_obj.mod))

editform = BranchForm(data=MultiDict(data_in))

if editform.validate_on_submit():
branch.name = editform.name.data

db.session.add(branch)
db.session.commit()

return redirect('/admin/branches/' + str(branch.id))

editform.name.data = branch.name

return render_template("branch_edit.html",
title="Edit Branch",
branch = branch,
editform = editform)

让我失望的是,在我使用 WTForm 表单并使用数据库中的数据填充字段(例如编辑表单)的其他地方,我必须在 form.validate_on_submit( ) block ,因为如果没有,则表单将永远不会更新,因为提交的任何内容都会立即被覆盖。

See "editform.name.data = branch.name" (this is how I've always done it)

从我在网上找到的有关填充 FieldList 的每个示例中,显然必须在实例化期间完成,但表单也必须在 validate_on_submit() 之前实例化,因为 validate_on_submit() 是表单对象的一种方法。

See "editform = BranchForm(data=MultiDict(data_in))" (this is how I've seen FieldLists populated in all the examples I've seen.)

如何使用字段列表填充表单?

最佳答案

好吧,有 friend 帮我解决了这个问题。这是我最终得到的结果:

表格:

class BranchForm(Form):
name = StringField('Name', validators = [Required()])
equipment = FieldList(SelectField('Equipment', validators=[Required()], coerce=int,
choices = [(x.id, x.name) for x in Equipment.query.all()]))
mod = FieldList(StringField('Method of Delivery', validators = [Optional()]))

def populate_assoc(self, branch_obj):
i = 0
branch_obj.name = self.name.data
for assoc_obj in branch_obj.equipment_assoc:
assoc_obj.equipment_id = self.equipment[i].data
assoc_obj.mod = self.mod[i].data
i += 1

查看:

def edit_branch(id):
branch = Branch.query.filter_by(id=id).first()

if request.method == 'POST':
editform = BranchForm()

if editform.validate_on_submit():
editform.populate_assoc(branch)

db.session.add(branch)
db.session.commit()

return redirect('/admin/branches/' + str(branch.id))

#populate data_in to be used
data_in = []
for eq_obj in branch.equipment_assoc:
data_in.append(('equipment', eq_obj.equipment.id))
data_in.append(('mod', eq_obj.mod))

editform = BranchForm(data=MultiDict(data_in))
editform.name.data = branch.name

return render_template("branch_edit.html",
title="Edit Branch",
branch = branch,
editform = editform)

真正的诀窍是不要使用 form.validate_on_submit() 作为我的逻辑分隔符,因为它依赖于表单对象。他的想法是使用 if request.method == 'POST': 来实现此目的。这样我就可以用两种不同的方式实例化我的表单。一个会填充以供显示,另一个仅在请求方法为 POST 时才会实例化,从而保留表单中提交的信息。

为了完成这项工作,我将 populate_assoc 方法添加到我的表单类中,以便我可以轻松地将表单中的信息放入我的关联模型中。

关于forms - 如何在 validate_on_submit() block 之后填充 WTForms FieldList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30519572/

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