I have a form to edit a 1:many set of data. I am using FieldList for the rows of related records. The form displays correctly (it's not pretty yet), and I can edit. On submit, I can access the parent record form.name.data but I cannot figure out how to access the child data in form.dqs (the fieldlist) to save them back to the db.
In validate_on_submit, I can see the form.name.data but not the data in form.dqs (the fieldlist).
I've tried everything... I looked through all the objects in the debugger and cannot see the actual data anywhere.
This is my first form like this.. I'm trying to stay in the Flask/WTF universe, but open to the best solution for any of what I'm trying to do. Thanks!
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>
I was able to solve this using this specific syntax:
demorptqs[i].qorder = form.dqs.entries[i]['qorder'].data
Could not get at the data any other way.