django-rest-framework [Intermediate] Override Pagination style and setup Pagination per class


Example

Override Pagination Style:

Every available pagination style can be overridden by creating a new class that inherits from one of the available styles and then alters its parameters:

class MyPagination(PageNumberPagination):
    page_size = 20
    page_size_query_param = 'page_size' 
    max_page_size = 200
    last_page_strings = ('the_end',)

Those parameters (as listed on the pagination official documentation) are:

PageNumberPagination

  • page_size: A numeric value indicating the page size. If set, this overrides the PAGE_SIZE setting. Defaults to the same value as the PAGE_SIZE settings key.
  • page_query_param: A string value indicating the name of the query parameter to use for the pagination control.
  • page_size_query_param: If set, this is a string value indicating the name of a query parameter that allows the client to set the page size on a per-request basis. Defaults to None, indicating that the client may not control the requested page size.
  • max_page_size: If set, this is a numeric value indicating the maximum allowable requested page size. This attribute is only valid if page_size_query_param is also set.
  • last_page_strings: A list or tuple of string values indicating values that may be used with the page_query_param to request the final page in the set. Defaults to ('last',)
  • template: The name of a template to use when rendering pagination controls in the browsable API. May be overridden to modify the rendering style, or set to None to disable HTML pagination controls completely. Defaults to "rest_framework/pagination/numbers.html".

LimitOffsetPagination

  • default_limit: A numeric value indicating the limit to use if one is not provided by the client in a query parameter. Defaults to the same value as the PAGE_SIZE settings key.
  • limit_query_param: A string value indicating the name of the "limit" query parameter. Defaults to 'limit'.
  • offset_query_param: A string value indicating the name of the "offset" query parameter. Defaults to 'offset'.
  • max_limit: If set this is a numeric value indicating the maximum allowable limit that may be requested by the client. Defaults to None.
  • template: Same as PageNumberPagination.

CursorPagination

  • page_size: Same as PageNumberPagination.
  • cursor_query_param: A string value indicating the name of the "cursor" query parameter. Defaults to 'cursor'.
  • ordering: This should be a string, or list of strings, indicating the field against which the cursor based pagination will be applied. For example: ordering = 'slug'. Defaults to -created. This value may also be overridden by using OrderingFilter on the view.
  • template: Same as PageNumberPagination.

Setup Pagination per Class:

In addition to the ability to setup the Pagination style globally, a setup per class is available:

class MyViewSet(viewsets.GenericViewSet):
    pagination_class = LimitOffsetPagination

Now only MyViewSet has a LimitOffsetPagination pagination.

A custom pagination style can be used in the same way:

class MyViewSet(viewsets.GenericViewSet):
    pagination_class = MyPagination