如何在没有装饰器的情况下继承烧瓶 MethodView 类? [英] How to inherit a flask MethodView class without its decorators?
问题描述
因为没有重写相同的API.我想从已经创建的 MethodView
继承 get 方法并忽略 login_required
decorator.
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'])
如果我向 /dostuffb
发出 GET 请求,
If I do a GET request to /dostuffb
,
需要认证吗?
Does it need to be authenticated?
我的继承语法是否正确?
Is my inheritance syntax correct?
推荐答案
View.decorators
列表仅在调用 View.as_view()
方法时应用.如果您不想在子类中应用任何装饰器,只需使用空序列覆盖该属性:
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
现在 DoStuffB.as_view()
将找到空元组而不是继承的 DoStuffA.decorators
列表,并且不应用任何装饰器.
Now DoStuffB.as_view()
will find the empty tuple rather than the inherited DoStuffA.decorators
list, and no decorators are applied.
这篇关于如何在没有装饰器的情况下继承烧瓶 MethodView 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!