[Flask] Getting values of Flask-WTF SelectMultipleField?

Alex Hall ahall at autodist.com
Wed Apr 20 15:10:09 EDT 2016


A small correction: I get one empty list, not two.

As I think about this more, do I need to set the id of each option element
somehow? If all of them were the same, that would make getlist("optionID")
work, wouldn't it? Is that the secret? I don't know if I can do that, since
the options are built from a list of tuples (value, text) as far as I
understand it.

On Wed, Apr 20, 2016 at 2:54 PM, Alex Hall <ahall at autodist.com> wrote:

> Thanks for the clarification. I used
> print request.form.getlist("users")
> but only got two empty lists (that is, []).
>
> I'm selecting users logged in when packages were processed at the company
> where I work. That is, which user did the processing for a given package. I
> want people to be able to check any users they want to include, to see only
> packages from those users. The function I have that queries the database
> expects a list of users, each user as a string. My selection list is set up
> this way, in my form class:
>
>  usernames = ["u1", "u2", "u3", "u4"]
> users = SelectMultipleField("Users", choices=[(u, u) for u in userNames])
>
> Then, in my template:
>
> <td>{{form.users(rows=6, multiple=True)}}</td>
>
> In the function handling this form's input (technically an Ajax handler,
> not a view, but it comes to the same thing):
>
> @app.route("/orderLookup", methods=searchMethods)
> def getOrderSearchResults():
>  results = []
>  print request.form.getlist("users")
>  for result in
> DBInterface.getOrderDetails(orderNumber=request.form["orderNumber"]):
>   results.append({
>    "name": result.user,
>    "orderNumber": result.reference_3
>   })
>  return json.dumps(results)
>
> Of course, I'll want to add the users list to the database lookup function
> once I can figure out how to access said list. Hopefully that clarifies
> things.
>
> On Wed, Apr 20, 2016 at 2:21 PM, David Lord <davidism at gmail.com> wrote:
>
>> And speaking of Flask-WTF, you should use that, as it handles multiple
>> values and many other things for you.
>>
>>
>> On 04/20/2016 11:20 AM, David Lord wrote:
>>
>> The same name is supplied multiple times with select multiple fields (and
>> this can be done with other fields as well). Use
>> request.form.getlist(name) to get all values for the given name.
>>
>> You found an example using Flask-WTF or just straight WTForms and
>> conflated it with request.form when it was really referring to form as
>> in “an instance of a Form“.
>>
>> On 04/20/2016 11:15 AM, Alex Hall wrote:
>>
>> Hello list,
>> I've only been using Flask for a few days, but I'm really likeing it so
>> far. My one question is how to deal with multiple select fields. I can get
>> the value of standard inputs with
>> request.form["fieldName"]
>> but I can't figure out what to do about multiple select fields. I've
>> googled the topic several different ways, but the only clear answer I found
>> suggested using
>> request.form.selectListName.data
>> which doesn't do anything.
>>
>> I need to get the selected values as a list to pass to a database lookup
>> function. For instance, the function expects ["v1", "v2", "v3"]. What's the
>> trick to accessing the selected values of this kind of field? Thanks!
>>
>> --
>> Alex Hall
>> Automatic Distributors, IT department
>> ahall at autodist.com
>>
>>
>> _______________________________________________
>> Flask mailing listFlask at python.orghttps://mail.python.org/mailman/listinfo/flask
>>
>>>>
>>
>>
>> _______________________________________________
>> Flask mailing list
>> Flask at python.org
>> https://mail.python.org/mailman/listinfo/flask
>>
>>
>
>
> --
> Alex Hall
> Automatic Distributors, IT department
> ahall at autodist.com
>



-- 
Alex Hall
Automatic Distributors, IT department
ahall at autodist.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160420/718f11aa/attachment-0001.html>


More information about the Flask mailing list