使用DRF缓存ViewSet:TypeError:_wrapped_view()

时间:2018-07-24 12:55:00

标签: django caching django-rest-framework memcached

我只想使用缓存到ViewSet的速度太慢:(,使用Django REST Framework。

我已经这样做了:

...
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie
...
class PRPKViewSet(viewsets.ModelViewSet):
    serializer_class = PrpkSerializer
    queryset = Prpk.objects.all().order_by('begin')
    # Authentification !
    permission_classes = (IsAuthenticated,)
    # Only 'get' method
    http_method_names = ['get']

    # Cache requested url for each user for 2 hours
    # @method_decorator(vary_on_cookie)
    @method_decorator(cache_page(60*2))
    def get_queryset(self):
        """ allow rest api to filter by submissions """
        queryset = Prpk.objects.all().order_by('begin')
        highway = self.request.query_params.get('highway', None)
        if highway is not None:
            queryset = queryset.filter(highway=highway)

        return queryset

但是在查询时,我遇到了这个错误:

TypeError: _wrapped_view() missing 1 required positional argument: 'request'

Memcached已安装。

因此,我可以仅缓存一个ViewSet(不使用扩展名吗?)?

非常感谢。

F。

1 个答案:

答案 0 :(得分:1)

装饰dispatch而不是get_queryset

@method_decorator(cache_page(60*2))
def dispatch(self, request, *args, **kwargs):
    return super().dispatch(request, *args, **kwargs)