Solution
class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, nullable=False)
class _SubForm(Form):
# The HiddenField later contains the id of the data record.
id = HiddenField('id')
excluded = SelectField('Exclude', choices=EXCLUDED_CHOICES, validators=[DataRequired()])
# The constructor is overwritten in order to bypass further fields for the csrf token.
def __init__(self, csrf_enabled=False, *args, **kwargs):
super(_SubForm, self).__init__(csrf_enabled=csrf_enabled, *args, **kwargs)
class EditForm(FlaskForm):
items = FieldList(FormField(_SubForm))
@app.route('/edit', methods=['GET', 'POST'])
def edit():
# Fill in the form with the necessary data.
items = Item.query.all()
form = EditForm(request.form, data={ 'items': items })
if form.validate_on_submit():
# Iterate over the FieldList here.
for field in form.items.data:
id = item_field.get('id')
excluded = item_field.get('excluded')
print(id, excluded)
return render_template('edit.html', **locals())