WTForms QuerySelectMultipleField不发送列表 [英] WTForms QuerySelectMultipleField Not sending List

查看:379
本文介绍了WTForms QuerySelectMultipleField不发送列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Flask应用程序上做一些事件调度。我在表单中遇到了WTForms QuerySelectMultipleField问题。



forms.py

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

在我的 init 解析表单POST数据。只是为了测试我试图返回request.form ['invitees']只是为了看看传递。最后我想验证数据并将其添加到我的SQLite3数据库。

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

WTForm文档说,QuerySelectMultipleField应该返回一个ORM模型实例的列表,但是当我解析POST请求时,我没有得到一个列表。我检查了我的浏览器中的POST数据,看起来像当我选择多个对象发送多个。

我似乎无法弄清楚这一点。任何帮助将不胜感激!

解决方案

如果您查询表单直接对象,而不是作为请求对象的一部分的原始表单数据

假设你使用Flask-WTF来构建它,那么你的被邀请者行应该读取被邀请者= form.invitees.data


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!

解决方案

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.

这篇关于WTForms QuerySelectMultipleField不发送列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆