如何在没有装饰器的情况下继承烧瓶 MethodView 类? [英] How to inherit a flask MethodView class without its decorators?

查看:35
本文介绍了如何在没有装饰器的情况下继承烧瓶 MethodView 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为没有重写相同的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,

  1. 需要认证吗?

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆