Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching a ViewSet with DRF : TypeError: _wrapped_view()

I just want to use caching to a ViewSet too slow :(, with Django REST Framework.

I've do this :

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

But when querying, I've this error :

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

Memcached is installed.

So, can I caching just one ViewSet (not using an extension ?) ?

Thanks a lot.

F.

like image 881
fabrice Avatar asked Dec 14 '22 15:12

fabrice


1 Answers

Decorate dispatch instead of get_queryset.

@method_decorator(cache_page(60*2))
def dispatch(self, request, *args, **kwargs):
    return super().dispatch(request, *args, **kwargs)
like image 94
Dušan Maďar Avatar answered Jan 05 '23 01:01

Dušan Maďar