1

我尝试将 req 解析器与 fields.String 枚举一起使用,以大摇大摆地显示一个不错的下拉列表:

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包装器,我确实很高兴地看到了这一点。但是当我尝试发送请求时,我收到了一个验证错误。调试时,我从该方法中得到一个error: String object is not callable提升:reqparse.convert

{
  "errors": {
    "local_seen": "'String' object is not callable"
  },
  "message": "Input payload validation failed"
}

这是为什么?我显然在参数下发送了一个字符串值。我究竟做错了什么?

4

2 回答 2

0

我将在github 问题上复制粘贴答案

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

我的建议,使用布尔值并在未指定时假设“全部”:

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
于 2016-01-27T14:04:18.613 回答
0

在 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
于 2016-01-28T08:23:05.850 回答