我在将装饰器应用到我的Resource
. 该Api
decorators
参数被应用于每个资源函数,或者我必须如何理解该decorators
参数?我的装饰器只在启动时应用,而不是在每个函数调用上。我究竟做错了什么?
from flask import Flask, Blueprint
from flask.ext.restplus import Api, Resource, fields
app = Flask(__name__)
def my_decorator(input):
print("my decorator")
return input
api_blueprint = Blueprint('index', __name__, url_prefix='/0')
api = Api(api_blueprint, version='0.1.0', title='Index API',
description='The Index API helps to find things faster in the own database',
# ui=False,
decorators=[my_decorator]
)
app.register_blueprint(api_blueprint)
ns = api.namespace('index', description='Index API')
search_id_score_model = api.model('id', {
'id': fields.String(required=True),
'score': fields.Float(required=True)
})
search_result_model = api.model('result', {
'key': fields.String(required=True),
'ids': fields.List(fields.Nested(search_id_score_model, required=True), required=True)
})
@ns.route('/search/<string:query>')
class Search(Resource):
'''Serach'''
@api.marshal_list_with(search_result_model)
@my_decorator
def get(self, query):
'''fetch result by query'''
return None
if __name__ == '__main__':
app.run(debug=True)