{"id":6610,"library":"drf-orjson-renderer","title":"Django RestFramework JSON Renderer Backed by orjson","description":"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.","status":"active","version":"1.8.0","language":"en","source_language":"en","source_url":"https://github.com/brianjbuck/drf_orjson_renderer","tags":["django","drf","json","performance","renderer","parser","orjson"],"install":[{"cmd":"pip install drf-orjson-renderer","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for high-performance JSON serialization/deserialization.","package":"orjson","optional":false},{"reason":"Required as a DRF renderer/parser.","package":"djangorestframework","optional":false}],"imports":[{"symbol":"ORJSONRenderer","correct":"from drf_orjson_renderer.renderers import ORJSONRenderer"},{"symbol":"ORJSONParser","correct":"from drf_orjson_renderer.parsers import ORJSONParser"}],"quickstart":{"code":"import os\n\n# settings.py\n\nREST_FRAMEWORK = {\n    \"DEFAULT_RENDERER_CLASSES\": (\n        \"drf_orjson_renderer.renderers.ORJSONRenderer\",\n        \"rest_framework.renderers.BrowsableAPIRenderer\",\n    ),\n    \"DEFAULT_PARSER_CLASSES\": (\n        \"drf_orjson_renderer.parsers.ORJSONParser\",\n        \"rest_framework.parsers.FormParser\",\n        \"rest_framework.parsers.MultiPartParser\",\n    ),\n    \"ORJSON_RENDERER_OPTIONS\": (\n        # Example options for orjson\n        # import orjson # You would import orjson in settings.py to use these\n        # orjson.OPT_NON_STR_KEYS,\n        # orjson.OPT_SERIALIZE_DATACLASS,\n        # orjson.OPT_SERIALIZE_NUMPY,\n    ),\n}","lang":"python","description":"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`."},"warnings":[{"fix":"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).","message":"When configuring `ORJSON_RENDERER_OPTIONS` in `settings.py`, ensure that any `orjson.OPT_*` flags are correctly imported from `orjson` within your settings file or defined as constants.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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`.","message":"For custom serialization of objects not natively supported by `orjson` (e.g., custom Python classes), you may need to provide a `default` function. This is typically done by overriding the `get_renderer_context()` method within your DRF `APIView` or `ViewSet`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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`.","message":"If you are manually creating and returning `ORJsonResponse` instances (rather than letting DRF's renderer handle it), you might encounter a `TypeError` for non-dictionary root objects. This is because `orjson` (and standard JSON) expects the top-level object to be a dictionary or list.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Version 1.8.0 specifically added support for NumPy 2.0. While `orjson` generally handles NumPy types, older `drf-orjson-renderer` versions might have had specific internal encoders or different compatibility with newer NumPy versions.","severity":"breaking","affected_versions":"<1.8.0"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}