繁体   English   中英

如何在validate_on_submit()块之后填充WTForms FieldList?

[英]How can you populate a WTForms FieldList after the validate_on_submit() block?

关于如何使用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()块之后填充这些表单字段,因为如果没有,那么表格永远不会更新,因为提交的内容会立即被覆盖。

请参阅“editform.name.data = branch.name”(这是我一直以来的做法)

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

请参阅“editform = BranchForm(data = MultiDict(data_in))”(这是我在所见的所有示例中看到的填充FieldLists的方法。)

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

好吧,所以一个伙伴帮我解决了这个问题。 这是我最终得到的:

形成:

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方法添加到我的表单类中,以便我可以轻松地将表单中的信息放入我的关联模型中。

WtForms有一个populate_obj()方法。 也许这就是你所追求的?

def edit_branch(id):
    branch = Branch.query.filter_by(id=id).first()
    editform = BranchForm(obj=branch)
    if editform.validate_on_submit():
        editform.populate_obj(branch)

        db.session.commit()

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM