Implementation code for Django REST framework paging

  • 2021-06-29 11:17:06
  • OfStack

Official Documents [Here]

Module for paging: Pagination

Django REST framework has a built-in Pagination module, no additional installation is required, just a simple configuration.

What to configure ?It tells Django what paging style to use, such as which fields to return, the size of each page, the name of the request parameter, and so on.

Two configurations:

1. Global configuration in settings.py file
2. Assign a custom page breaker to each view that needs to be paged separately.

Path 1 is the default configuration for all interfaces that inherit ListViewAPI, and Path 2 personalizes a single interface.

It is important to note that path 1 is globally configured and all interfaces that inherit ListAPIView will have paging by default. This affects the structure of the data returned by the existing interface. In addition to this, path 1 is quite good.

I prefer to use path 2 only. I can customize a generic page splitter and it's convenient to assign a page splitter only to view that needs to page.

Approach 1: Global configuration in settings.py file


REST_FRAMEWORK = {
 # ...
  'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
  'PAGE_SIZE': 100
}

DEFAULT_PAGINATION_CLASS: Default Pager (a subclass of BasePagination). Django provides three types: PageNumberPagination, LimitOffsetPagination, CursorPagination. Different types return different paging information. Or it can be your own custom pager.

PAGE_SIZE: Page size.

Path 2: Customize Pager

Here is an example of a process that uses a custom page breaker.

Create a new paginations.py for writing pagers:

We write a general page breaker in the paginations.py file:


class StandarPagination(PageNumberPagination):
  page_size = 20 #  Default number of bars per page configuration 
  page_query_param = 'page' #  Request parameter name for Number of Pages ,  Default is page
  page_size_query_param = 'page_size' #  Request parameter name for Page Size 
 
  #  Enter parent class  PageNumberPagination  Visible Response Volume Return Field .
  #  def get_paginated_response(self, data):
  #  return Response(OrderedDict([
  #    ('count', self.page.paginator.count),
  #    ('next', self.get_next_link()),
  #    ('previous', self.get_previous_link()),
  #    ('results', data)
  #  ]))

  
  #  Feeling inappropriate ,  Then copy it out , overloaded function ,  Add a few more fields yourself . 
  # ( You can learn which attributes to get the correct values from through official documentation or direct debugging .)
  def get_paginated_response(self, data):
    return Response(OrderedDict([
      ('count', self.page.paginator.count),
      ('next', self.get_next_link()),
      ('previous', self.get_previous_link()),
      ('page', self.page.number),
      ('total_page', self.page.paginator.num_pages),
      ('page_size', self.page.paginator.per_page),
      ('results', data)
    ]))

(See the documentation for more property descriptions)

Assign pager to interface, attribute pagination_class assignment is sufficient.


from rest_framework.pagination import PageNumberPagination

class MyListAPI(ListAPIView):
  queryset = TestModel.objects.filter()
  serializer_class = TestModelSerializer
  pagination_class = StandarPagination #  Newly added 

Done!

If the interface request address is http://api/test/

See how clients request interfaces:

http://api/test/: Return to page 1, page size is 20.

http://api/test/?page=2 & page_size=10: Return to page 2 with a page size of 10.


Related articles: