Flask - how do I combine Flask-WTF and Flask-SQLAlchemy to edit db models? Flask - how do I combine Flask-WTF and Flask-SQLAlchemy to edit db models? flask flask

Flask - how do I combine Flask-WTF and Flask-SQLAlchemy to edit db models?


Please refer to the wtforms documentation:

http://wtforms.simplecodes.com/docs/0.6/forms.html#wtforms.form.Form

You pass in the "obj" as argument. This will bind the model properties to the form fields to provide the default values:

@app.route('/person/edit/<id>/', methods=['GET', 'POST'])def edit_person(id):    person = Person.query.get_or_404(id)    form = PersonForm(obj=person)    if form.validate_on_submit():        form.populate_obj(person)

Notice also the "populate_obj" method. This is a handy shortcut which will bind the form values to the model properties (only those fields you have defined in your form, so quite safe to use).