如何在列表的解析参数中进行列表(字符串)的解析而不是列表(字符)的解析? [英] how to make parse of list(string) not list(char) in parse argument of list?
问题描述
我在烧瓶中使用flask_restful
I use flask_restful in flask
我的代码如下:
from flask_restful import Resource, reqparse
apilink_parser = reqparse.RequestParser()
apilink_parser.add_argument('provider_id', type=int,required=True)
apilink_parser.add_argument('name', type=str, required=True)
apilink_parser.add_argument('func_id', type=int)
apilink_parser.add_argument('method', type=str)
apilink_parser.add_argument('url', type=str)
apilink_parser.add_argument('parameter',type=list)
apilink_parser.add_argument("expectreturn", type=list)
@marshal_with(apilink_fields)
def post(self):
args = apilink_parser.parse_args()
print(args)
# user owns the task
task = APILink.create(**args)
return task, 201
我的json发布数据如下:
My json post data like:
{
"name":"riskiqwhois",
"provider_id":1,
"func_id":1,
"url":"myurl",
"parameter":["query"], //******//
"expectreturn":[],
"method":"post"
}
但是当我打印arrgs时,结果是:
but when I print the arrgs the result is:
{
'provider_id': 1,
'name': 'riskiqwhois',
'func_id': 1,
'method': 'post',
'url': 'myurl',
'parameter': ['q', 'u', 'e', 'r', 'y'], //******//
'expectreturn': None
}
我想要 您可以看到我想要参数是字符串列表,它只是一个名为"query"的元素,但是真正的参数转换为数据库是['q','u','e','r','y'] ,如何使参数是字符串列表而不是字符列表?如何确保数据为列表(字符串)?
I want You can see I want parameter is list of string which is only one element named "query", but the real parameter tranlate into the database is ['q', 'u', 'e', 'r', 'y'], How to make the parameter is list of string not list of char? how to make sure the data is list(string)?
推荐答案
您可以通过在请求解析器中添加 action ="append" 来解决此问题. 像下面一样
You can solve this problem by adding action="append" to your request parser like below
apilink_parser.add_argument('parameter',type=str,action="append")
apilink_parser.add_argument("expectreturn", type=list,action="append")
这将使您返回到输出以下
this will return you below output
{
'provider_id': 1,
'name': 'riskiqwhois',
'func_id': 1,
'method': 'post',
'url': 'myurl',
'parameter': ['query'],
'expectreturn': None
}
这篇关于如何在列表的解析参数中进行列表(字符串)的解析而不是列表(字符)的解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!