Dynamically adding fields to a Flask-wtf form.

1,108 views
Skip to first unread message

Tom Wright

unread,
Apr 5, 2017, 8:31:58 AM4/5/17
to pocoo-libs
I have a dynamically generated json object that defines the fields I want on a form. 

I have tried several ways to add the fields but keep hitting problems. Can anyone point me in the correct direction?

From the docs it seems this should work:

but complains that fields can't be added to an instance.

This https://pastebin.com/sCKZXeYs seems to work, until I try to render the form, when it complains the mySelect.choices doesn't exist.

Thanks for any clues.

Tom Wright

unread,
Apr 5, 2017, 9:24:05 AM4/5/17
to pocoo-libs
Ok, the problem was how I was building the SelectField

myField = SelectField('myselect')
myField.choices = [(1, 'a'), (2,'b')]

Doesn't work!!!

myField = SelectField('myselect', choices = [(1, 'a'), (2, 'b')])

Does :-)

Seb-a...@gmx.de

unread,
Feb 24, 2019, 3:47:59 AM2/24/19
to pocoo-libs
I think your previous way would be possible by using

myField = SelectField('myselect',[]) 

to create an empty List of options which you might fill afterwards. Note that it would be a good idea to insert some fallback value, as if the later assignment fails, the page would fail to load with an empty list given.
Reply all
Reply to author
Forward
0 new messages