CacheResponseMixin does not work with pagination

I added CacheResponseMixin from drf extensions to my view, but only the first page is cached and returned for all other pages, for example. /? page = 2 just returns the results for page 1.

class ProductViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    queryset = Product.objects.filter(withdrawn=False)
    serializer_class = ProductSerializer
    pagination_class = LargeResultsSetPagination

I am using django 1.85. Is this a mistake, or am I missing something?

+4
source share
2 answers

Final fix using a custom key constructor:

from rest_framework_extensions.cache.mixins import CacheResponseMixin
from rest_framework_extensions.key_constructor.constructors import (
    DefaultKeyConstructor
)
from rest_framework_extensions.key_constructor.bits import (
    QueryParamsKeyBit   
)

class QueryParamsKeyConstructor(DefaultKeyConstructor):
    all_query_params = bits.QueryParamsKeyBit()

class ProductViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    queryset = Product.objects.filter(withdrawn=False)
    serializer_class = ProductSerializer
    pagination_class = LargeResultsSetPagination
    list_cache_key_func = QueryParamsKeyConstructor()
0
source

This is not well documented, but when reading the source code (for the class PaginationKeyBit), it looks like you need to add either page_kwarg = 'page'or paginate_by_param = 'page'to the view class.

+1
source

Source: https://habr.com/ru/post/1649495/


All Articles