0

我有一个表格来编辑 1:many 数据集。我将 FieldList 用于相关记录的行。表单显示正确(还不是很漂亮),我可以编辑。提交时,我可以访问父记录 form.name.data 但我无法弄清楚如何访问 form.dqs (字段列表)中的子数据以将它们保存回数据库。

在 validate_on_submit 中,我可以看到 form.name.data,但看不到 form.dqs(字段列表)中的数据。

我已经尝试了一切......我查看了调试器中的所有对象,但在任何地方都看不到实际数据。

这是我的第一个这样的形式.. 我试图留在 Flask/WTF 宇宙中,但对我正在尝试做的任何事情的最佳解决方案持开放态度。谢谢!

class DemoReport(db.Model):
    __tablename__ = 'demoreports'
    id = db.Column(db.Integer, primary_key=True)
    org_id = db.Column(db.Integer)
    name = db.Column(db.String(80))
    demotypes = db.relationship('DemoType', backref='demoreport', lazy='dynamic') #  many:1 Demoreport:demotype
    demorptqs = db.relationship('DemoRptQ', backref='demoreport', lazy='dynamic') #  1:many Demoreport:demorptq

    def __repr__(self):
        return '<DemoReport %r>' % self.id

class DemoRptQ(db.Model):
    __tablename__ = 'demorptqs'
    id = db.Column(db.Integer, primary_key=True)
    org_id = db.Column(db.Integer)
    # many DemoRptQ's: 1 demoreport
    demoreport_id = db.Column(db.Integer, db.ForeignKey('demoreports.id'))
    # many demoreptQs: 1 question
    question_id = db.Column(db.Integer, db.ForeignKey('questions.id'))
    qorder = db.Column(db.Integer)
    def __repr__(self):
        return '<DemoRptQ %r>' % self.id
class DemoRptQForm(Form):
    qorder = IntegerField('Order')

class DemoReportForm(FlaskForm):
    name = StringField('Demo Report Name', validators=[Length(0, 80), DataRequired()])
    dqs = FieldList(FormField(DemoRptQForm), min_entries=10)
    submit = SubmitField('Submit')
@main.route('/demoreport/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_demoreport(id):
    if id==0:
        demoreport = DemoReport()
        demoreport.org_id = current_user.org_id
    else:
        demoreport = DemoReport.query.get_or_404(id)
        if demoreport.org_id != current_user.org_id:
            flash('That Demo Report is not in your Org.')
            return redirect(url_for('.demoreports_list'))

    demorptqs = DemoRptQ.query.filter_by(demoreport_id=demoreport.id).order_by(DemoRptQ.qorder).all()

    form = DemoReportForm()
    if form.validate_on_submit():
        demoreport.name = form.name.data
        for dq in form.dqs:
            db.session.add(dq)
        db.session.add(demoreport)
        db.session.commit()
        return redirect(url_for('.demoreports_list'))
    form.name.data = demoreport.name
    form.dqs = demorptqs
    return render_template('demoreport.html', form=form, demoreport=demoreport, demorptqs=demorptqs)
<form action method="post" class="form" role="form">
{{ form.hidden_tag() }}
<div class="container">
    <div class="row">
        <div class="col-md-6">
            {{ wtf.form_field(form.name, class='form-control', placeholder='Demo Template Name') }}
        </div>
    </div>
    <input class="btn btn-default" id="submit" name="submit" type="submit" value="Submit">
    {% for dq in form.dqs %}
        <div class="row">
            <input class="form-control" style="width: 10%" id="dq.qorder" name="dq.qorder" placeholder="Order" type="text" value="{{ dq.qorder }}">
            <div class="col-md-2>">{{ dq.question.qbody }}</div>
        </div>
    {% endfor %}
    <div class="col-md-6">
    </div>

</div>
</form>

在此处输入图像描述

4

1 回答 1

0

我能够使用以下特定语法解决此问题:

demorptqs[i].qorder = form.dqs.entries[i]['qorder'].data

无法以任何其他方式获取数据。

于 2021-06-29T22:21:20.693 回答