rest-framework finisher

A simple page (see n-Page, Display of n)

from rest_framework.pagination Import PageNumberPagination
 # a basic use: url = url = http: //127.0.0.1 :? 8000 / pager / page = 2 & size = 3, size is not valid 
class   Pager (APIView):
     DEF GET (Self, Request, * args, ** kwargs):
         # get all data 
        RET = models.Book.objects.all ()
         # create page objects 
        Page = PageNumberPagination ()
         # get paged data paginate_queryset method itself / have in the database, called directly 
        page_list page.paginate_queryset = (RET, Request, View = Self)
         # paging serializing 
        Ser = BookSerializer1 (= page_list instance, MANY = True)
        return the Response (ser.data)
 # two custom HTTP = url: //127.0.0.1: 8000 / pager / Page = 2 & size = 3? 
# size = 30, invalid, up to five 
class Mypage (PageNumberPagination):
     # page show how many 
    PAGE_SIZE = 2 # query that specifies a query key value is 
    page_query_param = ' Page ' # custom parameter passing / front control key value query page size = 9 how many such a display, the display 9 indicates a 
    page_size_query_param = ' size ' # maximum one data / control maximum display page number, size if transmission 100, the display is up to 10 
    MAX_PAGE_SIZE. 5 = class   Pager (APIView):
     DEF GET (Self, Request, * args, ** kwargs):
         # get all data
    
    
    

        = RET models.Book.objects.all ()
         # Create Object tab 
        Page = Mypage ()
         # acquired data pages in the database 
        page_list = page.paginate_queryset (RET, Request, View = Self)
         # paging serialized 
        ser = BookSerializer1 (= page_list instance, MANY = True)
         # return Response (ser.data) 
        # this is the return Response object, but more than the basic Previous, Next, and the total number of data (to understand) 
        return page.get_paginated_response (ser.data)

 settings

= REST_FRAMEWORK {
     # Showing two 
    ' PAGE_SIZE ' : 2 
}

Two offset tab (the n-th position, n data view rearwardly

# Http://127.0.0.1:8000/pager/?offset=4&limit=3 
from rest_framework.pagination Import LimitOffsetPagination
 # can also be customized, with simple paging 
class   Pager (APIView):
     DEF GET (Self, Request, * args, ** kwargs):
         # get all data 
        RET = models.Book.objects.all ()
         # create page objects 
        Page = LimitOffsetPagination ()
         # get paged in the database data 
        page_list = page.paginate_queryset (RET, Request, View = Self )
         # paging serializing 
        Ser = BookSerializer1 (= page_list instance, MANY = True)
         #page.get_paginated_response return (ser.data) 
        return the Response (ser.data) 

 two custom url = HTTP: //127.0.0.1: 8000 / pager / Page = 2 & size = 3?
 # size = 30, invalid, up to 5 
class myPage (LimitOffsetPagination):
     # from the pole position back to take several default taken 2, I can specify 
    default_limit = 2 # every acquisition number of 
    limit_query_param = ' limit ' # # benchmark values, now shifted to a position which, if offset = 6 in section 6 indicating the current position, next taking 
    offset_query_param = ' offset ' # maximum one data 
    max_limit. 5 = class   Pager (APIView):
     DEF GET (Self, Request, args *, **
    
    
    
kwargs):
         # Get all data 
        RET = models.Book.objects.all ()
         # create page objects 
        Page = Mypage ()
         # get paged in the database data 
        page_list = page.paginate_queryset (RET, Request, View = Self)
         # serializing paging 
        Ser = BookSerializer1 (= page_list instance, MANY = True)
         # return Response (ser.data) 
        # this is returned Response object, but substantially more than the previous, next, and the total data strip number (to understand) 
        return page.get_paginated_response (ser.data)

Three CursorPagination (encrypted page, only to see the previous and next, fast)

 

from rest_framework.pagination Import CursorPagination
 # see the source, through the sql query, greater than, and less than id id 
class   Pager (APIView):
     DEF GET (Self, Request, * args, ** kwargs):
         # Get all data 
        RET = models.Book .objects.all ()
         # create page objects 
        Page = CursorPagination ()
         # on what sort 
        page.ordering = ' NID ' 
        # in the database to obtain paging data 
        page_list = page.paginate_queryset (RET, Request, View = Self)
         # of tab serialized 
        ser = BookSerializer1 (instance = page_list, many =True)
         # avoid Page guessed 
        return page.get_paginated_response (ser.data)
 # If you want to customize the first inherited CursorPagination, then rewrite three parameters 
        # size display per page 
        page.page_size = 3 # query key value 
        = page.cursor_query_param ' the Cursor ' # on what sort 
        page.ordering = ' the above mentioned id '
        
        

 

Guess you like

Origin www.cnblogs.com/HUIWANG/p/11138878.html