1

Restframework 分页器 Pagnation 组件实例-5

 

分页逻辑

from rest_framework.pagination import PageNumberPagination

class BookView(APIView):

    # authentication_classes = [UserAuth]

    # permission_classes = [SVIPPermission]

    # throttle_classes = [VisitThrottle]

    # renderer_classes = []

    def get(self,request):
        '''
        查看所有书籍
        :param request:
        :return:
        '''

        class MyPageNumberPagination(PageNumberPagination):
            page_size=2
            page_query_param="page_num"
            page_size_query_param="size"
            max_page_size=5


        print(request.user,request.auth)

        book_list=Book.objects.all()

        pnp=MyPageNumberPagination()

        paged_book_list=pnp.paginate_queryset(book_list,request)

        serializer=BookSerializer(paged_book_list,many=True)

        return Response(serializer.data)

    def post(self,request):
        '''
        添加一条书籍
        :param request:
        :return:
        '''
        print(request.data)

        serializer=BookSerializer(data=request.data,many=False)

        if serializer.is_valid():
            serializer.save() # create操作

            return Response(serializer.data)
        else:
            return Response(serializer.errors)

  

 

 

posted @ 2018-09-21 16:58  萌哥-爱学习  阅读(292)  评论(0编辑  收藏  举报