Skip to content

Automatically generate JSON blobs in camelCase for the Django REST framework.

License

Notifications You must be signed in to change notification settings

hapytex/camel-drf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

camel-drf

Wraps Python's snake_case convention for fields to the camelCase convention in JSON blobs.

Python through PEP-8 and by extent Django typically have field names in snake_case, like user_name. Google's JSON style guideline on the other hand advises to use camelCase, so userName for field names. While one can manually wrap between the two styling conventions. This package aims to make it a bit more convenient: it provides two mixins: CamelCaseSerializerMixin and CamelAPIViewMixin. You should not use the two concurrently for the same serializer.8

If you have a serializer class where you want the presentation in JSON to use camelCase, you can use the CamelCaseSerializerMixin for this:

class MySerializer(CamelCaseSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = ['some_field_1', 'some_field_2']

The data generated by this serializer will then look like someField1 and someField2, and furthermore when validating data to create or update records, it will also process the data as camelCase data.8

One can also use a mixin at the APIView or ViewSet level by mixing the CamelCaseSerializerMixin in the APIView or ViewSet:

class MyModelViewSet(CamelCaseSerializerMixin, viewsets.ModelViewSet):
    pass

This will then, if there is a serializer_class specified that does not have the CamelCaseSerializerMixin as one of the subclasses, automatically create a second serializer (with the same name) that adds this as mixin.

This package has been constructed based on this StackOverflow answer.

About

Automatically generate JSON blobs in camelCase for the Django REST framework.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages