Given a Django model with a JSONField, what is the correct way of serializing and deserializing it using Django Rest Framework?
I\'ve already tried crating a custom
If you're using mysql (haven't tried with other databases), using both DRF's new JSONField
and Mark Chackerian's suggested JSONSerializerField
will save the json as a {u'foo': u'bar'}
string.
If you rather save it as {"foo": "bar"}
, this works for me:
import json
class JSONField(serializers.Field):
def to_representation(self, obj):
return json.loads(obj)
def to_internal_value(self, data):
return json.dumps(data)