django-rest-framework [Introductory] Setup Pagination Style Globally


Example

In order to set the pagination style for the entire project, you need to set the DEFAULT_PAGINATION_CLASS and PAGE_SIZE on the project settings.

To do so, go to settings.py and on the REST_FRAMEWORK variable, add the following:

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

In place of the DESIRED_PAGINATION_STYLE one of the following must be placed:

  • PageNumberPagination: Accepts a single page number in the request query parameters.

    http://your_api_url/a_table/?page=2
    
  • LimitOffsetPagination: Accepts a limit parameter, which indicates the maximum number of items that will be returned and an offset parameter which indicates the starting position of the query in relation to the dataset. PAGE_SIZE does not need to be set for this style.

    http://your_api_url/a_table/?limit=50&offset=100
    
  • CursorPagination: Cursor based pagination is more complex than the above styles. It requires that the dataset presents a fixed ordering, and does not allow the client to navigate into arbitrarily positions of the dataset.

  • Custom pagination styles can be defined in place of the above.