Django REST Framework Camel Case

1.4.2 · active · verified Tue Apr 14

djangorestframework-camel-case (version 1.4.2) is an active Python library that provides seamless camel case JSON support for Django REST framework. It automatically converts API request and response fields between Python's `snake_case` and JavaScript's `camelCase`, allowing developers to maintain consistent Pythonic naming conventions while exposing JavaScript-friendly APIs. The library was last updated in February 2023.

Warnings

Install

Imports

Quickstart

To quickly enable camel case conversion, configure the `DEFAULT_RENDERER_CLASSES` and `DEFAULT_PARSER_CLASSES` in your `settings.py` to use the provided camel case renderers and parsers. You can also add `CamelCaseMiddleWare` for query parameter conversion.

import os

# In your Django settings.py file
REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': [
        'djangorestframework_camel_case.render.CamelCaseJSONRenderer',
        'djangorestframework_camel_case.render.CamelCaseBrowsableAPIRenderer', # Optional
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ],
    'DEFAULT_PARSER_CLASSES': [
        'djangorestframework_camel_case.parser.CamelCaseJSONParser',
        'djangorestframework_camel_case.parser.CamelCaseFormParser', # Optional
        'djangorestframework_camel_case.parser.CamelCaseMultiPartParser', # Optional
        'rest_framework.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser',
    ],
}

# Optionally, add middleware for query parameter conversion
# In your Django settings.py file, in the MIDDLEWARE list
MIDDLEWARE = [
    # ... other middleware ...
    'djangorestframework_camel_case.middleware.CamelCaseMiddleWare',
    # ...
]

# Example of how you might integrate with a custom renderer (e.g., ORJSON)
# JSON_CAMEL_CASE = {
#     'RENDERER_CLASS': 'drf_orjson_renderer.renderers.ORJSONRenderer'
# }

# Example of customizing underscoreization for specific cases (e.g., v2Counter -> v2_counter)
# REST_FRAMEWORK = {
#     'JSON_UNDERSCOREIZE': {
#         'no_underscore_before_number': True,
#     },
#     # ... other DRF settings ...
# }

# A basic DRF serializer example (Python snake_case)
from rest_framework import serializers

class MyModelSerializer(serializers.Serializer):
    my_field_name = serializers.CharField(max_length=100)
    another_data_point = serializers.IntegerField()

# When rendered by CamelCaseJSONRenderer, it will appear as:
# {
#   "myFieldName": "value",
#   "anotherDataPoint": 123
# }

view raw JSON →