WTForms QuerySelectMultipleField Not sending List

情到浓时终转凉″ 提交于 2020-01-17 03:57:12

问题


I am working on a Flask Application to do some event scheduling. I am having problems with WTForms QuerySelectMultipleField in my form.

forms.py

class EnterEvent(Form):
...
invitees = QuerySelectMultipleField('Invitees', query_factory=lambda:
                            models.User.query.order_by(models.User.name).all())

and in my init.py file where I parse the Form POST data. Just to test I tried to return request.form['invitees'] just to see what is passed. Eventually I want to validate the data and add it to my SQLite3 DB.

@app.route('/event', methods=['POST', 'GET'])
def addEvent():
    form = EnterEvent()
    if request.method == 'POST':
        ...
        invitees = request.form['invitees']
        return invitees

the WTForm docs say that the QuerySelectMultipleField should return a list with ORM model instances but when I parse the POST request I am not getting a list. I checked the POST data in my browser and it looks like when I select multiple objects it is sending more than one.

I can't seem to figure this out. Any help will be greatly appreciated!


回答1:


You would get your ORM model instances if you query your form object directly, rather then the 'raw' form data that's part of the request object

Assuming you're using Flask-WTF with it's little helpers built it, your invitees line should really read invitees = form.invitees.data.



来源:https://stackoverflow.com/questions/17499369/wtforms-queryselectmultiplefield-not-sending-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!