{
"errors": {
"number": "number 'str' object is not callable"
},
"message": "Input payload validation failed"
}
没明白哪里出问题了,如有大神看到帮忙纠错。
我的代码很简单,基本上也是按照demo来的,参照Michał Karzyński老兄的博文:http://michal.karzynski.pl/blog/2016/06/19/building-beautiful-restful-apis-using-flask-swagger-ui-flask-restplus/
# api/v1/parsers.py
from flask_restplus import reqparse
planlist_arguments = reqparse.RequestParser()
planlist_arguments.add_argument('number', type='str', required=False, help='change number')
planlist_arguments.add_argument('mindate', type='str', required=False, help='start date')
planlist_arguments.add_argument('maxdate', type='str', required=False, help='end date')
然后在apidemo.py中调用
# api/v1/apidemo.py
from flask import jsonify, request
from project.api.v1.parsers import planlist_arguments
...
@api.route('/planlist')
@api.param('number', 'number')
@api.param('mindate', 'start date')
@api.param('maxdate', 'end date')
class GetPlanList(Resource):
"""
API
"""
@api.doc('Get_Plan_List')
@api.response(HTTP_BAD_REQUEST, 'need at least one argument')
@api.expect(planlist_arguments)
@api.marshal_with(record_model)
def get(self):
"""query by number or start end date"""
args = planlist_arguments.parse_args(request)
changenumber = args.get('changenumber')
mindate = args.get('mindate')
maxdate = args.get('maxdate')
pass
没太明白为何不可调用,难道是request的问题?但是request不是全局上下文定义的么?不然会是什么问题呢?
这个模块本来很强大,我是很想用的,但是这当头一棒,让我摸不着头脑,看了RequestParser的源码依然不明,已先弃用此参数校验功能,这个轮子看来是用不上了,还是自己辛苦点造个吧
FLASK-RESTPLUS的文档:https://flask-restplus.readthedocs.io/en/stable/_modules/flask_restplus/reqparse.html