drf-orjson-renderer is a highly performant JSON renderer and parser for Django Rest Framework (DRF), leveraging the `orjson` library, which is backed by Rust for speed, safety, and correctness. It significantly boosts API response times by replacing DRF's default JSON renderer and also supports pretty-printed JSON for the Browsable API. The current version is 1.8.0, with ongoing active maintenance and updates.
pip install drf-orjson-rendererVerified import paths — ran on the pinned version, not inferred.
To quickly integrate `drf-orjson-renderer`, add `ORJSONRenderer` and `ORJSONParser` to your `DEFAULT_RENDERER_CLASSES` and `DEFAULT_PARSER_CLASSES` respectively in your Django `settings.py`. You can also configure `orjson` specific serialization options using `ORJSON_RENDERER_OPTIONS`.
Import `orjson` directly in `settings.py` to reference `orjson.OPT_*` flags, or define the integer values of these flags if `orjson` cannot be directly imported at `settings.py` parse time (though direct import is generally preferred and recommended in the official documentation).
Override `get_renderer_context` in your view and pass a custom `default` callable in the context that `orjson` can use to serialize unrecognized types. For example: `context['default'] = your_custom_serializer_func`.
When instantiating `ORJsonResponse` directly with a non-dictionary or non-list object as the top level, you must pass `safe=False` (e.g., `ORJsonResponse(data, safe=False)`). The library's renderer handles this automatically, but direct usage requires explicit handling. Ensure your response data is serializable by `orjson`.
Upgrade to `drf-orjson-renderer` version 1.8.0 or higher to ensure full compatibility with NumPy 2.0 and potentially improved handling of NumPy data types.