paging

This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

Thank you for meeting you. I’m Y Dazhuang

By Y Dazhuang Link: juejin.cn/user/756923… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

🌊🌈

Part of the article and pictures from the Internet, if you have any questions please contact me

🌊🌈

1 Global paging

We can set global paging in the configuration file, for example:

REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', 'PAGE_SIZE': 100 # number of pages}Copy the code

Note: If you turn off paging in the view, just set it in the view

pagination_class = None
Copy the code

2 Local paging

# * * * * * * * * * * * * * * * * * page * * * * * * * * * * * * * * * * * * * * from rest_framework. Response import response from rest_framework. Pagination import PageNumberPagination from . import models class PageSerializer(serializers.ModelSerializer): class Meta: model = models.Role fields = '__all__' class PageGroupView(APIView): def get(self, request, *args, **kwargs): roles = models.Role.objects.all() pg = PageNumberPagination() data = pg.paginate_queryset(queryset=roles,request=request,view=self) pg_data = PageSerializer(instance=data, many=True) return Response(pg_data.data)Copy the code
url(r'^(? P < version > / v1 | v2 +)/page1 / $', views. PageGroupView. As_view ()), # pagingCopy the code

3 Optional pager (custom paging)

1)PageNumberPagination

Attributes that can be defined in subclasses:

  • Page_size Number of pages
  • Page_query_param Number of pages sent by the front end keyword, default “page”
  • Page_size_query_param Number of pages sent by the front end Keyword. Default is None
  • Max_page_size Maximum number of pages that can be set by the front end

Paging, look at the n page, each page shows N data;

Class MyPageNumberPagination (PageNumberPagination) : "" "http://127.0.0.1:8000/api/v1/page1/? Page = 2 "" "page_size = 2 page_size_query_param = 'page_size' # http://127.0.0.1:8000/api/v1/page1/? Page = 1 & page_size = 6 to specify each page shows article number max_page_size = 5 class PageSerializer (serializers. ModelSerializer) : class Meta: model = models.Role fields = '__all__' class PageGroupView(APIView): def get(self, request, *args, **kwargs): Roles = models.role-objects.all () # create pagination object pg = MyPageNumberPagination() # create pagination object pg = MyPageNumberPagination Pg.paginate_query (querySet =roles,request=request,view=self) pg_data = PageSerializer(instance=data, many=True) return Response(pg_data.data)Copy the code

2)LimitOffsetPagination

Attributes that can be defined in subclasses:

  • Default_limit Specifies the default limitPAGE_SIZESettings have been
  • Limit_query_param limit Parameter name, default ‘limit’
  • Offset_query_param offset parameter name, default ‘offset’
  • Max_limit Maximum limit. Default: None

Paging, looking back at n data in N locations;

Class MyLimitOffsetPagination (LimitOffsetPagination) : "" "http://127.0.0.1:8000/api/v1/page1/? Offset =3&limit=4 # display 4 rows of data on page 3 "" default_limit = 2 # Default 2 rows of data on page 2 limit_query_param = 'limit' # 'offset' biggest max_limit = 5 # # showed several data page 5 class PageSerializer (serializers. ModelSerializer) : class Meta: model = models.Role fields = '__all__' class PageGroupView(APIView): def get(self, request, *args, **kwargs): Roles = models.role-objects.all () # create a pagination object # pg = MyLimitOffsetPagination() # Data = pg.paginate_querySet (querySet =roles, request=request, Pg_data = PageSerializer(instance=data, Many =True) # return Response(pg_data.data) return pg.get_paginated_response(pg_data.data) # Add some parametersCopy the code

3)CursorPagination

Encrypted pages, previous and next

Class MyCursorPagination (CursorPagination) : "" encryption" paging "next" : "http://127.0.0.1:8000/api/v1/page1/? Cursor = cD00 ", "previous" : "http://127.0.0.1:8000/api/v1/page1/? cursor=cj0xJnA9Mw%3D%3D", "" cursor_query_param = 'cursor' # query parameter page_size = 2 # order = 'id' # query with id. -id: reverse order page_size_query_param = None # name max_page_size = None article # largest number class PageSerializer (serializers. ModelSerializer) : class Meta: model = models.Role fields = '__all__' class PageGroupView(APIView): def get(self, request, *args, **kwargs): Roles = models.role-objects.all () # create pagination object # pg = mypagberPagination () # pg = MyLimitOffsetPagination() pg Data = pg.paginate_querySet (queryset=roles, request=request, Pg_data = PageSerializer(instance=data, Many =True) # return Response(pg_data.data) return pg.get_paginated_response(pg_data.data) # Add some parametersCopy the code