4

我已将 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。这是一个错误还是我错过了什么?

4

2 回答 2

1

这没有很好的记录,但是阅读源代码(对于PaginationKeyBit该类)看起来您需要添加page_kwarg = 'page'或添加paginate_by_param = 'page'到您的视图集类。

于 2016-08-10T09:13:56.360 回答
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()
于 2016-08-30T09:20:33.993 回答