[Django]-JSONField serializes as json for POST, but string for GET

6๐Ÿ‘

โœ…

You can override the to_internal_value and to_representation in a new serializer field to handle the return data for JSON field.

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 in turn, you would use this Field in a serializer:

class SomeSerializer(serializers.ModelSerializer):
    json_field = JSONSerializerField()

    class Meta:
        model = SomeModelClass
        fields = ('json_field', )

This should solve your problem ๐Ÿ™‚

๐Ÿ‘คuser8904707

0๐Ÿ‘

When I originally created the columns I did it with a different json field package. The base DB columns was actually text instead of json or jsonb. Creating new columns (django json fields), migrating the data, and then shifting the data back got my database back in a consistent order.

๐Ÿ‘คJohn Rake

Leave a comment