Search code examples
pythondjangogetdeserialization

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[]')

Solution

  • Just retrieve all values with the getlist() method:

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

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