Skip to main content

DRF Pagination Meta Wrap

Wrap your DRF pagination meta data

Table of Contents

Installation

  1. Install the package using pip:
pip install drf-pagination-meta-wrap
  1. Add drf_pagination_meta_wrap to INSTALLED_APPS
INSTALLED_APPS = [
    ...,
    "drf_pagination_meta_wrap",
]

Usage

Extend the Pagination class with WrapPaginationMetadataMixin class provideed by the package.

Lets take an example with BlogPagination class.

class BlogPagination(WrapPaginationMetadataMixin, PageNumberPagination):
    pass

Now the response will look like:

{
    "meta": {
        "count": 10,
        "next": "http://127.0.0.1:8000/api/blogs/?page=2",
        "previous": null
    },
    "results": [...]
}

This works for all pagination classes.

Providing Additional Meta

If you want to add additional meta data to the meta key of the response, you can set the additional_metadata in the WrapPaginationMetadataMixin class.

class BlogPagination(WrapPaginationMetadataMixin, PageNumberPagination):
    additional_metadata = {
        "foo": "bar",
    }

Or you can use the get_additional_metadata method.

class BlogPagination(WrapPaginationMetadataMixin, PageNumberPagination):
    def get_additional_metadata(self):
        return {
            "foo": "bar",
        }

Now the pagination response will have another foo key in the meta.

Note: if you need to generate the OpenApi Schema for the additional metadata, read additional metadata schema

How it works

The WrapPaginationMetadataMixin mixin wraps everything in meta key except results.

Customizing

Custom Meta Key

You can customize the meta key. By default it is meta. You can customize it by setting the paginated_response_meta_key in the class.

class BlogPagination(WrapPaginationMetadataMixin, PageNumberPagination):
    paginated_response_meta_key = "metadata"

Or if you want it globally, you can set the PAGINATED_RESPONSE_META_KEY in the package settings.

DRF_PAGINATION_META_WRAP = {
    "PAGINATED_RESPONSE_META_KEY" = "metadata"
}

Custom Data Key

You can customize the data key. By default it is results. You can customize it by setting the paginated_response_data_key in the class.

class BlogPagination(WrapPaginationMetadataMixin, PageNumberPagination):
    paginated_response_data_key = "data"

Or if you want it globally, you can set the PAGINATED_RESPONSE_DATA_KEY in the package settings.

DRF_PAGINATION_META_WRAP = {
    "PAGINATED_RESPONSE_DATA_KEY" = "data"
}

OpenApi Schema Generation

The WrapPaginationMetadataMixin mixin generates the OpenApi schema for the pagination response.

Additional Metadata Schema

If you have additional metadata, the mixin will try to auto generate its schema by introspecting the get_additional_metadata method. This may not work properly for all cases.

For example if your Pagination class is used in a authenticated request context, it will fail.

In this case, you have to manually provide the additional_metadata properties in get_additional_metadata_properties_schema method.

class BlogPagination(WrapPaginationMetadataMixin, PageNumberPagination):
    additional_metadata = {
        "foo": "bar",
    }

    def get_additional_metadata_properties_schema(self):
        return {
            "foo": {
                "type": "string",
                "example": "bar",
            },
        }

Contributing

Contributions are more than welcome! Please open an issue if you have any questions or suggestions.

Metadata

Release files for drf-pagination-meta-wrap 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-pagination-meta-wrap 0.2
File Size Uploaded Provenance Actions
drf_pagination_meta_wrap-0.2.tar.gz 5.2 kB Without provenance Details for drf_pagination_meta_wrap-0.2.tar.gz

Release files / drf_pagination_meta_wrap-0.2.tar.gz

Download URL drf_pagination_meta_wrap-0.2.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
08dc1cd5d63fcc2b6476013a489b6e6c6c91077c3efbe876d78b2cbb189bfaf2
BLAKE2b-256 checksum
How to use checksums
a7238d13a7ce511a542f98c1aa98ba923c3c54232a9f3632c71911fca1dc5b61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Security for drf-pagination-meta-wrap 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file No provenance

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