Python 使用dict作为RequestParser验证参数
我想知道是否有办法将dict传递给RequestParser的参数 它通常是这样工作的Python 使用dict作为RequestParser验证参数,python,flask-restful,Python,Flask Restful,我想知道是否有办法将dict传递给RequestParser的参数 它通常是这样工作的 my_parser = reqparse.RequestParser() my_parser.add_argument('my_field', type=dict, help='my_field must be a dict', required=True) my_arguments = {type=dict, help='my_field must be a dict', requ
my_parser = reqparse.RequestParser()
my_parser.add_argument('my_field', type=dict, help='my_field must be a dict', required=True)
my_arguments = {type=dict, help='my_field must be a dict', required=True}
出于代码可重用性的目的,我想这样使用它
my_parser = reqparse.RequestParser()
my_parser.add_argument('my_field', type=dict, help='my_field must be a dict', required=True)
my_arguments = {type=dict, help='my_field must be a dict', required=True}
然后使用以下命令将这些作为参数传递给.add_参数函数:
my_parser.add_argument('my_field', my_arguments)
我在尝试使用列表理解时遇到了麻烦
my_parser.add_arguments('my_field', list(key=value for (key, value) in parser_arguments.items()))
在这一点上,我意识到我可能无法使用dict,可能需要执行getattr(),显然在这方面投入了太多时间。对我来说,这似乎是可能的,也是优雅的,但无论如何,我都找不到解决办法,感谢在这方面有任何启发性的想法
编辑:我不能使用直观的
my_parser.add_argument('my_field', my_arguments)
因为dict将被添加到解析器参数的“default”字段,而实际字段(“type”、“help”和“required”)不受影响。:
这相当于
my_parser.add_argument('my_field', type=dict, help='my_field must be a dict', required=True)
可能是重复的我发誓我试过了,当我试过的时候,一定是口述本身有错误。这显然非常有效。