[英]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. 目前,我只是使用
replace
和split
来处理字符串以获取具有所有值的序列,但是我很好奇Python或Django中是否没有某些Utility / helper方法来执行此操作。
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. 显然,Django在处理请求时可以做非常相似的事情,但是我a)不知道是否/如何使用它,以及b)是否可以从
foo[]
表示法接收序列。
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()
): 我的值来自POST,所以我做了以下操作(不确定是否有更直接的方法,例如
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. 那是PHP或Rails的习惯用法,在Django中没有位置。
To get a list from a repeated parameter, though, you can just use getlist
: 但是,要从重复的参数获取列表,您可以只使用
getlist
:
foos = request.GET.getlist('foo[]')
Just retrieve all values with the getlist()
method : 只需使用
getlist()
方法检索所有值:
foo_list = request.GET.getlist('foo[]')
Include the brackets, they are just part of the name. 包括方括号,它们只是名称的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.