尝试在 flask-restplus 中将 fields.String 与枚举一起使用时引发字符串不可调用异常
String not callable exception raised when trying to use fields.String with enum in flask-restplus
我尝试将请求解析器与 fields.String 枚举一起使用,以在 swagger 中显示一个漂亮的下拉列表:
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=fields.String(enum=['true', 'false', 'all'], default='all')
@seen_api.route('/')
class SeenSearchAPI(APIResource):
@api.response(404, 'Page does not exist')
@api.response(200, 'Successfully retrieved seen objects', seen_search_schema)
@api.doc(parser=seen_search_parser)
def get(self, session):
""" Search for seen entries """
args = seen_search_parser.parse_args()
我使用 expect
包装器,我确实很自信地看到了这一点。但是当我尝试发送请求时出现验证错误。调试时,我从 reqparse.convert
方法中得到一个 error: String object is not callable
:
{
"errors": {
"local_seen": "'String' object is not callable"
},
"message": "Input payload validation failed"
}
这是为什么?我显然在参数下发送了一个字符串值。我做错了什么?
我会 copy-paste 在 github issue
上回答
RequestParser
期望来自 flask_restplus.inputs
模块的类型。 (参见 https://github.com/noirbizarre/flask-restplus/blob/master/flask_restplus/inputs.py#L372-L393 示例)
您可以自己编写,在您的情况下:
def local_seen(value):
if value not in ('true', 'false', 'all'):
raise ValueError('Unexpected value {0}'.format(value))
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=local_seen, default='all',
help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen == 'true':
# true case
elif local_seen == 'false':
# false case
elif local_seen == 'all':
# all case
我的建议是,使用布尔值并在未指定时假设 'all':
from flask_restplus.inputs import boolean
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=boolean, help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen is True:
# true case
elif local_seen is False:
# false case
elif local_seen is None:
# all case
在 git 问题中回答:
我忘了但是 RequestParser 也可以处理字符串枚举
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=str, default='all',
choices=('true', 'false', 'all'),
help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen == 'true':
# true case
elif local_seen == 'false':
# false case
elif local_seen == 'all':
# all case
我尝试将请求解析器与 fields.String 枚举一起使用,以在 swagger 中显示一个漂亮的下拉列表:
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=fields.String(enum=['true', 'false', 'all'], default='all')
@seen_api.route('/')
class SeenSearchAPI(APIResource):
@api.response(404, 'Page does not exist')
@api.response(200, 'Successfully retrieved seen objects', seen_search_schema)
@api.doc(parser=seen_search_parser)
def get(self, session):
""" Search for seen entries """
args = seen_search_parser.parse_args()
我使用 expect
包装器,我确实很自信地看到了这一点。但是当我尝试发送请求时出现验证错误。调试时,我从 reqparse.convert
方法中得到一个 error: String object is not callable
:
{
"errors": {
"local_seen": "'String' object is not callable"
},
"message": "Input payload validation failed"
}
这是为什么?我显然在参数下发送了一个字符串值。我做错了什么?
我会 copy-paste 在 github issue
上回答RequestParser
期望来自 flask_restplus.inputs
模块的类型。 (参见 https://github.com/noirbizarre/flask-restplus/blob/master/flask_restplus/inputs.py#L372-L393 示例)
您可以自己编写,在您的情况下:
def local_seen(value):
if value not in ('true', 'false', 'all'):
raise ValueError('Unexpected value {0}'.format(value))
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=local_seen, default='all',
help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen == 'true':
# true case
elif local_seen == 'false':
# false case
elif local_seen == 'all':
# all case
我的建议是,使用布尔值并在未指定时假设 'all':
from flask_restplus.inputs import boolean
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=boolean, help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen is True:
# true case
elif local_seen is False:
# false case
elif local_seen is None:
# all case
在 git 问题中回答:
我忘了但是 RequestParser 也可以处理字符串枚举
seen_search_parser = api.parser()
seen_search_parser.add_argument('page', type=int, default=1, help='Page number')
seen_search_parser.add_argument('max', type=int, default=100, help='Seen entries per page')
seen_search_parser.add_argument('local_seen', type=str, default='all',
choices=('true', 'false', 'all'),
help='Filter list by local status.')
class MyResource(Resource):
@api.expect(seen_search)
def get(self):
args = seen_search..parse_args()
local_seen = args['local_seen']
if local_seen == 'true':
# true case
elif local_seen == 'false':
# false case
elif local_seen == 'all':
# all case