POST data for camelCase in the Django REST Framework

I see that there is a project called djangorestframework-camel-case that allows you to use JavaScript-ish camelCase with underscore_cased fields in Django REST serializers. So basically, I can send:

{ "camelCase": "foo" } 

And get it with the following Serializer:

 class MySerializer(serializers.Serializer): session_id = serializers.CharField() 

Is there something similar for POST data? So, can I send camelCase=foo via POST and get it in the underscore in my serializer?

I tried to implement my own parser based on FormParser:

 class CamelCaseFormParser(FormParser): media_type = 'application/x-www-form-urlencoded' def __init__(self): print("initialized") def parse(self, stream, media_type=None, parser_context=None): print("parse") ... 

And by adding it to DEFAULT_PARSER_CLASSES in settings.py, while initialized is actually printed, parse not. Thus, it seems that in the case of POST data, the application/x-www-form-urlencoded parser is not used at all.

Since serializers are used like this:

 Serializer(data=request.data) 

I am thinking of subclassing Serializer and changing data before it is processed further, or even changing it before creating Serializer. But what I ask is a more convenient way that works for all serializers, without subclassing them.

+5
source share
1 answer

why not stick to parsers?

 from djangorestframework_camel_case.util import underscoreize from rest_framework import parsers from django.conf import settings from django.http import QueryDict class CamelCaseFormParser(parsers.FormParser): def parse(self, stream, media_type=None, parser_context=None): parser_context = parser_context or {} encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET) data = QueryDict(stream.read(), encoding=encoding) return underscoreize(data) 

simple, working and properly placed ...

+3
source

All Articles