简体   繁体   English

flask reqparse 如何使用 action='append' 获取空列表(默认情况下不是)

[英]flask reqparse how to get empty list using action='append' (not by default)

i need to get empty list if it in request but also need to get None if argument don't given at all in request如果在请求中,我需要获取空列表,但如果在请求中根本没有给出参数,我还需要获取 None

if i defined argument like that如果我这样定义论点

parser.add_argument('participating',type=int, nullable=False, action='append')

then if i do request like this那么如果我确实要求这样

print(post('http://localhost:80/api/', json={'participating':[]}).json())

parser sees it like 'participating':None解析器将其视为“参与”:无

and if i do that request如果我提出这个要求

print(post('http://localhost:80/api/', json={}).json())

then parser also sees it like 'participating':None然后解析器也将其视为“参与”:无

i solved it by check for exist key in flask.request.json我通过检查 flask.request.json 中的存在密钥来解决它

if not args['participating'] and 'participating' in flask.request.json and not flask.request.json['participating']:
    args['participating'] = flask.request.json['participating']

in that case values was:在那种情况下,价值是:
args['participating'] = None args['参与'] = 无
flask.request.json['participating'] = [] flask.request.json['参与'] = []
(but i open for any others solutions) (但我愿意接受任何其他解决方案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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