问题
For the reason of not rewriting the same API. I want to inherit a get method from an already created MethodView
and ignore the login_required
decorator.
class DoStuffA(MethodView):
decorators = [login_required]
def get(self):
return jsonify({"status":"ok"})
api.add_url_rule('/dostufa', view_func=DoStuffA.as_view("dostuffa"), methods=['GET'])
class DoStuffB(DoStuffA):
pass
api.add_url_rule('/dostuffb', view_func=DoStuffB.as_view("dostuffb"), methods=['GET'])
If I do a GET request to /dostuffb
,
Does it need to be authenticated?
Is my inheritance syntax correct?
回答1:
The View.decorators
list is applied only when the View.as_view()
method is called. If you don't want any decorators to be applied in your subclass, just override the attribute with an empty sequence:
class DoStuffB(DoStuffA):
decorators = () # empty tuple
Now DoStuffB.as_view()
will find the empty tuple rather than the inherited DoStuffA.decorators
list, and no decorators are applied.
来源:https://stackoverflow.com/questions/41652544/how-to-inherit-a-flask-methodview-class-without-its-decorators