簡體   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