I decided to solve this problem by overriding Django the included JSON serializer, in particular the handle_field method, in the custom serializer file custom_json_serializer.py. By doing this, I can guarantee that specific JSONFields remain as they are, without being converted to a string.
In case anyone else encounters this problem, these are the steps I took. I had to add this custom serializer to the settings.py file:
SERIALIZATION_MODULES = { 'custom_json': 'myapp.utils.custom_json_serializer', }
and then call it when serializing data from Django:
python manage.py dumpdata mymodel --format=custom_json --indent=2 --traceback > mymodel_data.json
A custom serializer looks like this:
from django.core.serializers.json import Serializer as JSONSerializer from django.utils.encoding import is_protected_type
The really strange part is that before this fix, some JSONFields were serialized just fine, while others were not. This is why I took the approach of defining fields for processing. Now all data is correctly serialized.
Ian
source share