How to inherit a flask MethodView class without its decorators?

你说的曾经没有我的故事 提交于 2019-12-12 03:44:28

问题


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,

  1. Does it need to be authenticated?

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!