简体   繁体   中英

Python/Django: How to deserialize foo[]=1&foo[]=2&

Is there a pre-built deserializer for query strings like:

foo[]=1&foo[]=2&foo[]=34&....&foo[]=5

Currently I am just working the string with replace and split to get a sequence with all the values, but I am curious if there isn't some utility/helper method in Python or Django to do this.

Obviously Django is able to do very similar things when processing requests, but I a) don't know if/how I can use it and b) if it is possible to receive a sequence from the foo[] notation.

Note: I am not able to change the source string, since it originates from a third party package.

UPDATE

Thanks for your replies. My values come from POST, so I did the following (not sure if there is a more direct way, like the answers used with request.GET.getlist() ):

foo = QueryDict(request.POST.get('my_var_name')).getlist('foo[]')

Firstly, there's absolutely no reason to send data with the [] prefix. That's a PHP or Rails idiom, and has no place in Django.

To get a list from a repeated parameter, though, you can just use getlist :

foos = request.GET.getlist('foo[]')

Just retrieve all values with the getlist() method :

foo_list = request.GET.getlist('foo[]')

Include the brackets, they are just part of the name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM