Any good practice to caching ModelViewSet?

749 views
Skip to first unread message

ali marefati

unread,
Mar 6, 2020, 5:15:09 AM3/6/20
to Django REST framework
i try to cache ModelViewSet:

my view is :

class CommentsViewSet(ModelViewSet):
queryset = CommentsModel.objects.all()
serializer_class = CommentsALLSerializer
http_method_names = ['get']
permission_classes = (AllowAny,)


i try this but not work:
    @method_decorator(cache_page(60*60))
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

how to deal with it ?
















Anne-Sophie Le Bloas

unread,
Mar 6, 2020, 2:08:26 PM3/6/20
to django-res...@googlegroups.com
Hi Ali,
Have you tried cacheops ?
It makes caching very easy.
Anne-Sophie

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/605c8643-53eb-456a-afba-e102ca44dd97%40googlegroups.com.


--
Anne-Sophie Le Bloas

Ngô Hoàng

unread,
May 22, 2021, 11:18:23 AM5/22/21
to Django REST framework
don't use it on DISPATCH.
use on GET or POST instead

@method_decorator(cache_page(60*60*2))
def list(self, *args, **kwargs):
return super().list(*args, **kwargs)
Reply all
Reply to author
Forward
0 new messages