user1383029
user1383029

Reputation: 2125

Caching on Django Rest Framework GenericViews

I don`t find infos on how caching of Django Rest Framework generic views is configured. Do I have to override the get message and add an own @method_decorator? This feels ungeneric to me.

class BlogTags(generics.ListAPIView):
    queryset = CustomContentBlogTag.objects.all()
    serializer_class = CustomContentBlogTagSerializer
    permission_classes = [AllowAny]

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

Docs I found:
Caching: https://www.django-rest-framework.org/api-guide/caching/
Generic views: https://www.django-rest-framework.org/api-guide/generic-views/

In the generic views docs, there is a sentence, that the queryset is cached somehow. But what, if I want the whole view to be cached?

Upvotes: 0

Views: 465

Answers (1)

michauwilliam
michauwilliam

Reputation: 845

You can use the name parameter on method_decorator and decorate a class. In fact when you use it on a class, the name is required.

Example:

@method_decorator(cache_page(60 * 5), name="list")
class MyView(mixins.ListModelMixin, GenericViewSet):
    ...

Upvotes: 1

Related Questions