day95-paging component

day95-paging component

1. Paging according to the page number and the number of data on each page, the actual configuration can

from rest_framework import pagination


# Paging according to the page number and how many data on each page
class MyPagination(pagination.PageNumberPagination):
    """
    http://127.0.0.1:8000/api/page?page=2&size=3
    Display from the second page, each page displays 3 data
    """
    # One page shows two pieces of data
    page_size = 2
    
    # url Splicing query keywords, page=n, starting from page n
    page_query_param ='page'
    
    # url Splicing query keywords, size=m, each page displays m pieces of data, no more than max_page_size
    page_size_query_param ='size'
    
    # Each page gives you a limit of three pieces of data, even if there are more than three pieces of data per request, only 3 pieces of data will be displayed
    max_page_size = 3

1.1 Enter PageNumberPagination to view other configurable others

2. Paging according to the nth data and the amount of data per page, the actual configuration is fine

# Paging according to the nth data and the amount of data per page
class LimitOffsetPaginator(pagination.LimitOffsetPagination):
    """
    http://127.0.0.1:8000/api/page?offset=2&limit=4
    Starting from the 3rd data, each page shows 4 data
    """
    # Each page displays 1 piece of data by default
    default_limit = 1
    # url Splicing query keywords, limit=n, how many items are displayed on each page
    limit_query_param ='limit'
    # url Splicing query keywords, offset=m, start to display from the m+1th data
    offset_query_param ='offset'

2.1 Enter LimitOffsetPagination to view other configurable others

3. Paging according to the cursor, only the previous page and the next page

# Paging according to the cursor, only the previous page and the next page
class CursorPaginator(pagination.CursorPagination):
    cursor_query_param ='cursor'
    # Make a cursor based on id
    ordering ='id'
    # Each page shows 2 pieces of data
    page_size = 2

3.1 Enter CursorPagination to view other configurable others

4. To use the pager in the view, first import the pager written by yourself

class PageBookView(APIView):
    def get(self, request):
        queryset = Book.objects.all()

        # Instantiate this paging class
        page_obj = pagination.MyPagination()
        # page_obj = pagination.LimitOffsetPaginator()
        # page_obj = pagination.CursorPaginator()

        # Queryset of each page, model
        page_queryset = page_obj.paginate_queryset(queryset, request)

        # The paging model of each page serializes the data
        ser_obj = BookSerializer(page_queryset, many=True)

        # This method encapsulates some links to the previous page and the next page
        return page_obj.get_paginated_response(ser_obj.data)

5. It can also be used and configured directly in the three-layer package view

# Encapsulate the global APIView for the third time
class BookModelView(viewsets.ModelViewSet):
    # Also configure queryset and serializer_class
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    pagination_class = pagination.MyPagination
    # pagination_class = pagination.LimitOffsetPaginator
    # pagination_class = pagination.CursorPaginator

5.1 Note that DRF directly provides inherited views.ModelViewSet, not written by day93.

5.2 Enter views.ModelViewSet, you can see that it has actually helped us inherit all the classes directly. What we do is configuration work

Reference: https://cloud.tencent.com/developer/article/1597273 day95-Paging component- Cloud+Community- Tencent Cloud