I'm using WTForms model forms in flask and if I use it to edit an existing record all the data is submitted to the db successfully, however if you then go back to edit the record again, the checkboxes for the Boolean fields always default back to unchecked again, even though the data has been submitted correctly. However the other fields (dates, text, etc) do display the data, so the view is passing the record to form correctly. I'm letting WTF manage the fields and input field type, so there's very little to get wrong! None of these are foreign keys so it's a really simple setup...
Any ideas with the checkboxes/Boolean fields aren't playing ball when all the others are? TIA
model.py
class Tests(db.Model):
__tablename__ = "tests"
id = Column(Integer, primary_key=True)
myboolean_field = Column(Boolean())
my_date = Column(Date)
notes = Column(String(500), nullable=True)
forms.py
class EditTestForm(ModelForm):
class Meta:
model = Tests
include_foreign_keys = True
routes.py
def edit_test(id):
testdetail=Tests.query.filter_by(id=id).first()
form=EditTestForm(request.form, testdetail)
if request.method == "POST" and form.validate():
form.populate_obj(obj=testdetail)
db.session.commit()
flash('Record updated!')
return redirect(url_for('bp.rd_view_test', id=id))
return render_template('edit_test.html', form=form, testdetail=testdetail)
Aucun commentaire:
Enregistrer un commentaire