在Django rest框架中缓存queryset?

2024-10-01 17:41:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大的查询集,还有一个泛型的分页器。ListAPIView。我想用它的所有页面缓存queryset,但我认为这样不行。我一直在试图找出如何使用分页器缓存queryset,但似乎没有找到一种方法让它与Django Rest一起工作

这是我的分页类:

class StandardPagesPagination(PageNumberPagination):
  page_size = 35

  def get_paginated_response(self, data):
      return Response({
          'links': {
              'next': self.get_next_link(),
              'previous': self.get_previous_link()
          },
          'count': self.page.paginator.count,
          'total_pages': self.page.paginator.num_pages,
          'results': data
      })

这是我的观点:

class OyunlarList(generics.ListAPIView):
    # queryset = Oyunlar.objects.all()
    filter_backends = [DjangoFilterBackend,filters.SearchFilter,filters.OrderingFilter]
    search_fields=['title']
    filterset_fields=['categories__name','platform','categories__category_id','game_id']
    ordering_fields = ['title', 'release_date','click_count','popularite']
    pagination_class = StandardPagesPagination


    serializer_class = OyunlarSerializer
    def get_queryset(self):
        queryset=Oyunlar.objects.all().order_by('click_count').prefetch_related('categories')
        #queryset=Oyunlar.objects.raw("select 'game_id' AS id,'title','base_price','en_ucuz' from oyunlar ")


        return queryset

    @method_decorator(cache_page(60 * 3))
    def dispatch(self, *args, **kwargs):
        return super(OyunlarList, self).dispatch(*args, **kwargs)

我想给用户一个更流畅的体验


Tags: selfidfieldsgetreturnobjectstitledef

热门问题