Skip to main content

drf-standardized-responses

PyPI CI/CD PyPI Downloads Python Version License

A Django REST Framework utility for standardized API responses, pagination, and exception handling.


Features

  • Consistent Response Format: All API responses follow a standardized structure (success, message, data, with optional meta and errors fields).
  • Custom Renderer: A StandardResponseRenderer that formats all responses consistently, preventing common issues like double-wrapping.
  • Standardized Pagination: StandardPagination class that provides rich metadata, including count, total_pages, and current_page.
  • Custom Exception Handler: A handler that catches DRF exceptions and formats them into the standard error response structure.
  • Helper Functions: Utilities to easily create standardized success and error responses.

Standard Response Structure

Success Response

{
  "success": true,
  "message": "Operation successful",
  "data": {
    "id": 1,
    "name": "Item 1"
  }
}

Success Response with Pagination

{
  "success": true,
  "message": "Operation successful",
  "data": [
    {
      "id": 1,
      "name": "Item 1"
    }
  ],
  "meta": {
    "pagination": {
      "count": 100,
      "next": "http://api.example.org/items?page=2",
      "previous": null,
      "total_pages": 10,
      "current_page": 1,
      "page_size": 10
    }
  }
}

Error Response

{
  "success": false,
  "message": "Validation failed",
  "data": {},
  "errors": {
    "name": ["This field is required"],
    "email": ["Enter a valid email address"]
  }
}

Installation

pip install drf-standardized-responses

Quick Start

Add the following to your Django settings.py:

REST_FRAMEWORK = {
    # Use the custom exception handler
    'EXCEPTION_HANDLER': 'drf_standardized_responses.exceptions.standardized_exception_handler',

    # Use the custom pagination class
    'DEFAULT_PAGINATION_CLASS': 'drf_standardized_responses.pagination.StandardPagination',

    # Use the custom renderer
    'DEFAULT_RENDERER_CLASSES': [
        'drf_standardized_responses.renderers.StandardResponseRenderer',
        # Add other renderers if needed, like BrowsableAPIRenderer
        'rest_framework.renderers.BrowsableAPIRenderer',
    ],
}

API Reference

StandardResponse

A utility class for creating standardized API responses.

from drf_standardized_responses.responses import StandardResponse

# Create a success response
response = StandardResponse.success(
    data={"key": "value"},
    message="Data retrieved successfully",
    meta={"custom_meta": "value"},
    status_code=200
)

# Create an error response
response = StandardResponse.error(
    message="Validation failed",
    errors={"field": ["This field is required"]},
    status_code=400
)

StandardResponseRenderer

This renderer automatically wraps your API responses in the standard structure. It correctly handles both error and success responses.

StandardPagination

A pagination class that integrates with the standardized response format to provide consistent pagination metadata.

standardized_exception_handler

An exception handler that catches DRF exceptions and formats them into standardized error responses.

Testing

Run the test suite:

pytest

Author


Contributing

Contributions are welcome! Please check out the contributing guidelines for more details on how to contribute to this project.


License

BSD-3-Clause

Metadata

Release files for drf-standardized-responses 0.1.4

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-standardized-responses 0.1.4
File Size Uploaded
drf_standardized_responses-0.1.4.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-standardized-responses 0.1.4
File Interpreter ABI Platform
drf_standardized_responses-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / drf_standardized_responses-0.1.4.tar.gz

Download URL drf_standardized_responses-0.1.4.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
26d62cb09ccfa93299ea82c7df6b3ac2423df1defe76f24875b67f186db07721
BLAKE2b-256 checksum
How to use checksums
a187a23d3c97dc640b165fc05d02bbe51f3ff34c9b5c671d6946762b33150ed7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / drf_standardized_responses-0.1.4-py3-none-any.whl

Download URL drf_standardized_responses-0.1.4-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
41af793fe2e1513cc239e4224d78873f157ee4c3b8e3a3a224e4ff0980d366c6
BLAKE2b-256 checksum
How to use checksums
6e404d282369de222e8441c026599f24d8f29ac0caa19077f343167fd5c45cd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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