Django CacheResponseMixin不适用于分页
我已经将来自drf扩展的CacheResponseMixin添加到我的视图集中,但是只有第一个页面被缓存并为所有其他页面返回,例如/?page=2只是返回第1页的结果Django CacheResponseMixin不适用于分页,django,django-rest-framework,drf-extensions,Django,Django Rest Framework,Drf Extensions,我已经将来自drf扩展的CacheResponseMixin添加到我的视图集中,但是只有第一个页面被缓存并为所有其他页面返回,例如/?page=2只是返回第1页的结果 class ProductViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet): queryset = Product.objects.filter(withdrawn=F
class ProductViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
queryset = Product.objects.filter(withdrawn=False)
serializer_class = ProductSerializer
pagination_class = LargeResultsSetPagination
我使用的是django 1.85。这是一个bug还是我遗漏了什么?这没有很好的文档记录,但是阅读源代码(对于
PaginationKeyBit
类),您似乎需要将page\u kwarg='page'
或paginate\u by\u param='page'
添加到您的viewset类中。使用自定义密钥构造函数的最终修复:
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()