Skip to content Skip to sidebar Skip to footer

Update Model With Wtforms Form Data

I have some Flask-SQLAlchemy models and Flask-WTF forms generated with wtforms_alchemy to represent them. I implemented a method on each model to update its attributes from a form'

Solution 1:

Use the form's populate_obj method to fill in the model. It sets an attribute of the same name as each field.

form.populate_obj(car)
db.session.commit()

If the simple "set attribute by field name" behavior isn't appropriate for a given model/form pair (although it should be in your case), you can override the method.

classSpecialCarForm(FlaskForm):
    ...

    defpopulate_obj(obj):
        # mess with data, set extra fields, etc.# potentially call super aftersuper().populate_obj(obj)

Post a Comment for "Update Model With Wtforms Form Data"