如何从Decorator中添加/修改属性到基于Django类的视图?

时间:2014-04-07 04:28:38

标签: python django django-class-based-views python-decorators

我有一个基于类的Django视图如下:

class myView(TemplateView):
    template_name = 'templateFile.html'
    request = None

    @method_decorator(request_management)
    def dispatch(self, request, *args, **kwargs):
        ...
        return super(myView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, *args, **kwargs):
        ctx = super(newFeatures, self).get_context_data(**kwargs)
        ctx['requester'] = self.requester
        return ctx

在我的request_management装饰器中,我想将myView.request的值设置为传递给dispatch函数的参数。所以我做了这样的事情:

def request_management(function):
    @wraps(function)
    def decorator(*args, **kwargs):
        logger.debug("request = %s" % str(args[0]))
        # I want to say here something like: 
        # self.request = args[0]
        # but of course, "self" is not defined in this context.
        return function(*args, **kwargs)
    return decorator

但是从装饰者那里,我无法访问" self"装饰方法的实例。 如何获取该实例并将名为request的属性附加到该实例,以便我可以在该实例的其他方法中使用该属性?

1 个答案:

答案 0 :(得分:2)

method_decorator的目的是convert a function decorator into a method decorator。但是既然你正在编写自己的装饰器,你可以继续把它写成一个真正的方法装饰器:

class myView(TemplateView):

    @request_management
    def dispatch(self, request, *args, **kwargs):
        ... 
        return super(myView, self).dispatch(request, *args, **kwargs)

    def request_management(method): 
        @wraps(method)
        def decorator(self, request, *args, **kwargs):
            logger.debug("request = %s" % str(request))
            self.request = request

            return method(self, request, *args, **kwargs)

        return decorator