Django Rest Framework APIView中的分页控件

时间:2019-04-10 16:55:33

标签: python django pagination django-rest-framework

我试图向我的自定义视图添加分页,并且分页可以工作,但是我没有此分页控件来更改页面,就像默认分页样式一样,如何将这个控件添加到视图中?

我一直在寻找解决这个问题的方法,但是我几乎什么也没找到,通常是当我找到某种向自定义视图添加分页的方法时,看起来像这样,但这不是我想要的,因为这没有用更改页面的可能性。

pagination.py

class CustomPagination(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'page_size'

    def get_paginated_response(self, data):
        return Response({
            'next': self.get_next_link(),
            'previous': self.get_previous_link(),
            'count': self.page.paginator.count,
            'limit': self.page_size,
            'result': data
        })

views.py

class TaskIndexAPIView(APIView):

    def filter_queryset(self, queryset):
        for obj in list(self.filter_backends):
            queryset = obj().filter_queryset(self.request, queryset, self)
            return queryset

    filter_backends = (filters.SearchFilter,)
    search_fields = ('name', 'description', 'user__username')

    def get_queryset(self):
        return Task.objects.all()

    def get(self, request):
        filtered_qs = self.filter_queryset(self.get_queryset())
        tasks = filtered_qs
        for i in tasks:
            if i.date <= date.today() and i.status == 'NEW':
                i.delayed = 'This task is delayed'
                i.save()
            else:
                i.delayed = ''
                i.save()
        pagination_class = CustomPagination()
        page = pagination_class.paginate_queryset(tasks, request)
        serializer = IndexSerializer(page, many=True)
        return Response(serializer.data)

0 个答案:

没有答案