Skip to main content

DRF Link Navigation Pagination

Yet another thirdy party package for DRF Pagination.

This package adds support to some headers that allow you to configure the behaviour of the LimitOffsetPagination class.

X-Drf-Change-Domain

If you want to change de host address located on next and previous fields, then this is the right thing to you. Take this body as an example:

{
  "links": {
    "next": "http://localhost/api/v1/entities/?limit=1&offset=2",
    "previous": "http://localhost/api/v1/entities/limit=1"
  },
  "count": 100,
  "results": [
    {
      "id": "bc6c6868-4e4d-4381-a785-f353ee7ecce5"
    }
  ]
}

Using a reverse proxy (like AWS API Gateway) to intermediate between your API, adding X-Drf-Change-Domain=chumaco header to the request will make the client receive the following:

{
  "links": {
    "next": "http://chumaco/api/v1/entities/?limit=1&offset=2",
    "previous": "http://chumaco/api/v1/entities/limit=1"
  },
  "count": 100,
  "results": [
    {
      "id": "bc6c6868-4e4d-4381-a785-f353ee7ecce5"
    }
  ]
}

X-Drf-Max-Pagination-Size

This header allows you to define different limits to the pagination depending on the gateway you are exposing your API. For example. For client A we allow a max of 50 entries, for client B we allow a max of 100 you can by injecting this header on the client's requests.

If for some reason the client exceeds the limit he will receive a Bad Request Error:

{
    "detail": "Bad limit value sent.",
    "error": {
        "code": "bad_limit_value"
    },
    "type": "API_EXCEPTION"
}

Otherwise the default paginated response is generated:

{
  "links": {
    "next": "https://chumaco/api/v1/entities/?limit=1&offset=2",
    "previous": "https://chumaco/api/v1/entities/limit=1"
  },
  "count": 100,
  "results": [
    {
      "id": "bc6c6868-4e4d-4381-a785-f353ee7ecce5"
    }
  ]
}

Configuration process

To use this package after installing you need to use it's pagination class. in your settings.py do the following:

REST_FRAMEWORK = {
    "DEFAULT_PAGINATION_CLASS": "drf_link_navigation_pagination.LinkNavigationPagination",
    ...
}

Tests

In order to execute the tests:

pipenv run tox

Metadata

Release files for drf-link-navigation-pagination 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-link-navigation-pagination 1.0.2
File Size Uploaded
drf_link_navigation_pagination-1.0.2.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-link-navigation-pagination 1.0.2
File Interpreter ABI Platform
drf_link_navigation_pagination-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 19.4 kB

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page