Python-django Rest Framework Framework Page

Source: Internet
Author: User

1. Previously Django-made paging components, when the amount of data is particularly large, performance is not very high, there are three ways to handle:
A. Record the last data ID of the current access page, and fetch the number of entries
B. Display up to 120 pages
C. Encrypt page numbers (show previous page, next page only)

2.rest Framework Paging
From rest_framework.pagination import limitoffsetpagination,pagenumberpagination,cursorpagination
A. Do paging based on limit offsetclassP1 (limitoffsetpagination): Max_limit= 3#maximum number of bars per pageDefault_limit = 2#number of bars per pageLimit_query_param ='Limit'Offset_query_param='Offset' #Simple Paging classIndexview (views. Apiview):defGet (self,request,*args,**Kwargs): User_list=models. UserInfo.objects.all () P1=P1 () page_user_list= P1.paginate_queryset (Queryset=user_list, Request=request, view=Self ) Ser= Indexserializer (Instance=page_user_list, many=True)returnResponse (Ser.data)#do not include the previous page and the next page, to manually pass in the URL limit and offset to control the first page #return P1.get_paginated_response (ser.data) # with previous page and next page #A brief example of pagination in a company classIndexview (views. Apiview):defGet (self,request,*args,**Kwargs): Ret=Baseresponse ()Try: User_list=models. UserInfo.objects.all () P1=P1 () page_user_list= P1.paginate_queryset (queryset=user_list,request=request,view=Self ) Ser= Indexserializer (instance=page_user_list,many=True) Ret.data=Ser.data Ret.next=P1.get_next_link () ret.*** =See the source to achieve their ownexceptException as E:ret.code= 1001Ret.error='xxxx Error' returnResponse (ret.__dict__B. Page-based paginationclassP2 (pagenumberpagination):#number of data bars displayed per pageMax_page_size = 5page_size= 2Page_size_query_param='size' #Page NumberPage_query_param ='page' #usage is the same as above classIndexview (views. Apiview):defGet (self,request,*args,**Kwargs): Ret=Baseresponse ()Try: User_list=models. UserInfo.objects.all () P1=P2 () page_user_list= P1.paginate_queryset (queryset=user_list,request=request,view=Self ) Ser= Indexserializer (instance=page_user_list,many=True) Ret.data=Ser.data Ret.next=P1.get_next_link () ret.*** =See the source to achieve their ownexceptException as E:ret.code= 1001Ret.error='xxxx Error' returnResponse (ret.__dict__c. Cursor-based paging: Record the ID of the last data, how many entries are taken back, and then encryptclassP3 (cursorpagination): Cursor_query_param='cursor'page_size= 2Ordering='ID' #sorting can also be written-id #usage is the same as above classIndexview (views. Apiview):defGet (self,request,*args,**Kwargs): Ret=Baseresponse ()Try: User_list=models. UserInfo.objects.all () P1=P3 () page_user_list= P1.paginate_queryset (queryset=user_list,request=request,view=Self ) Ser= Indexserializer (instance=page_user_list,many=True) Ret.data=Ser.data Ret.next=P1.get_next_link () ret.*** =See the source to achieve their ownexceptException as E:ret.code= 1001Ret.error='xxxx Error' returnResponse (ret.__dict__)

Python-django Rest Framework Framework Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.