zoukankan      html  css  js  c++  java
  • REST framwork之分页器,路由器,响应器

    一 REST framwork分页器:

    from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination
    
    class PNPagination(PageNumberPagination):
            page_size = 1    
            page_query_param = 'page'
            page_size_query_param = "size"
            max_page_size = 5
    
    class BookViewSet(viewsets.ModelViewSet):
    
        queryset = Book.objects.all()
        serializer_class = BookSerializers
        def list(self,request,*args,**kwargs):
    
            book_list=Book.objects.all()
            pp=LimitOffsetPagination()
            pager_books=pp.paginate_queryset(queryset=book_list,request=request,view=self)
            print(pager_books)
            bs=BookSerializers(pager_books,many=True)
    
            #return Response(bs.data)
            return pp.get_paginated_response(bs.data)

    二 路由器:

    在view视图中我们已经将代码进行最大化的简化,但是有一个问题就是我们的urls 中不得不进行传参,以区别不同的GET请求,分别为:Retrieve与List

        url(r'^authors/$', views.AuthorModelView.as_view({"get": "list", "post": "create"})),
        url(r'^authors/(?P<pk>d+)/$',views.AuthorModelView.as_view({"get": "retrieve", "put": "update", "delete": "destroy"})),

    使用了REST framwork路由器:

    三 响应器:

  • 相关阅读:
    游标cursor
    SQL: EXISTS
    LeetCode Reverse Integer
    LeetCode Same Tree
    LeetCode Maximum Depth of Binary Tree
    LeetCode 3Sum Closest
    LeetCode Linked List Cycle
    LeetCode Best Time to Buy and Sell Stock II
    LeetCode Balanced Binary Tree
    LeetCode Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/zhangsanfeng/p/9432213.html
Copyright © 2011-2022 走看看