我已将drf-extensions中的CacheResponseMixin添加到我的视图中,但只有第一页被缓存并返回所有其他页面,例如/?page = 2只返回第1页的结果。
class ProductViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
queryset = Product.objects.filter(withdrawn=False)
serializer_class = ProductSerializer
pagination_class = LargeResultsSetPagination
我正在使用django 1.85。这是一个错误还是我错过了什么?
答案 0 :(得分:1)
这没有详细记录,但是阅读源代码(对于PaginationKeyBit
类),您需要在视图集类中添加page_kwarg = 'page'
或paginate_by_param = 'page'
。
答案 1 :(得分:0)
使用自定义键构造函数进行最终修复:
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()