Django Rest Framework and JSONField

If you’re using Django Rest Framework >= 3.3, then the JSONField serializer is now included. This is now the correct way.

If you’re using Django Rest Framework < 3.0, then see gzerone’s answer.

If you’re using DRF 3.0 – 3.2 AND you can’t upgrade AND you don’t need to serialize binary data, then follow these instructions.

First declare a field class:

from rest_framework import serializers

class JSONSerializerField(serializers.Field):
    """ Serializer for JSONField -- required to make field writable"""
    def to_internal_value(self, data):
        return data
    def to_representation(self, value):
        return value

And then add in the field into the model like

class MySerializer(serializers.ModelSerializer):
    json_data = JSONSerializerField()

And, if you do need to serialize binary data, you can always the copy official release code

Leave a Comment