OK - I know this has been posted but I'm still having a bit of trouble applying some of the explanations. This is mentioned here as part of "Dynamic Form Composition" http://wtforms.simplecodes.com/docs/1.0.1/specific_problems.html
I want a form dynamically created from a list of users stored in my postgres database via sqlalchemy:
for user in Users:
user = SelectField('user', coerce=int)
That's it - simply create a bunch of drop-down list form elements each having the name of the user so I can get the data per user later (form.user.data). Populating the data in the drop-down list is easy -- it's static information.
Now if NOT using WTForms - this is simple, I can do all of this in Jinja2. However since I define my form FIRST with WTForms I'm having trouble.
This brings back to the link I posted. It shows:
def my_view():
class F(MyBaseForm):
pass
F.username = TextField('username')
for name in iterate_some_model_dynamically():
setattr(F, name, TextField(name.title()))
form = F(request.POST, ...)
Assuming my forms are defined in forms.py, views in views.py and html rendered in web page by jinja2 that is presented to user can't get the above working.
If anyone has a more real-world example that applies it would be greatly appreciated. In the meantime I'll keep hacking away.