Skip to main content

No project description provided

Project description

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.

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_pagination_meta_wrap-0.2.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file drf_pagination_meta_wrap-0.2.tar.gz.

File metadata

File hashes

Hashes for drf_pagination_meta_wrap-0.2.tar.gz
Algorithm Hash digest
SHA256 08dc1cd5d63fcc2b6476013a489b6e6c6c91077c3efbe876d78b2cbb189bfaf2
MD5 652bda7dd628892326c226ca8ab84bc1
BLAKE2b-256 a7238d13a7ce511a542f98c1aa98ba923c3c54232a9f3632c71911fca1dc5b61

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page