I’m using JSONField in some of my Django models and would like to migrate this data from Oracle to Postgres.
So far I haven’t had any luck keeping this JSON data intact when using Django’s dumpdata and loaddata commands, the data is transformed into string representations of the JSON. I’ve yet to find a good solution to this… Ideas?
I ended up solving this problem by overriding Django’s included JSON serializer, specifically the handle_field method, in a custom serializer file called custom_json_serializer.py. By doing this I can ensure that specific JSONFields stay as is, without being converted to string.
On the chance anyone else runs into this issue, these are the steps I took. I had to add this custom serializer to the settings.py file:
and then call it when serializing the data from Django:
The custom serializer looks like:
The really strange part is that before this fix some JSONFields were serializing just fine, while others were not. That is why I took the approach of specifying the fields to be handled. Now all data is serializing correctly.