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