问题
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