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.
Decorate dispatch
instead of get_queryset
.
@method_decorator(cache_page(60*2))
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With