Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django JSONField dumping/loading

Tags:

json

django

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?

like image 257
Growth Mindset Avatar asked May 16 '11 19:05

Growth Mindset


1 Answers

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:

SERIALIZATION_MODULES = {       
    'custom_json': 'myapp.utils.custom_json_serializer',
}

and then call it when serializing the data from Django:

python manage.py dumpdata mymodel --format=custom_json --indent=2 --traceback > mymodel_data.json

The custom serializer looks like:

from django.core.serializers.json import Serializer as JSONSerializer
from django.utils.encoding import is_protected_type

# JSONFields that are normally incorrectly serialized as strings
json_fields = ['problem_field1', 'problem_field2']


class Serializer(JSONSerializer):
    """
    A fix on JSONSerializer in order to prevent stringifying JSONField data.
    """
    def handle_field(self, obj, field):
        value = field._get_val_from_obj(obj)
        # Protected types (i.e., primitives like None, numbers, dates,
        # and Decimals) are passed through as is. All other values are
        # converted to string first.
        if is_protected_type(value) or field.name in json_fields:
            self._current[field.name] = value
        else:
            self._current[field.name] = field.value_to_string(obj)

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.

like image 158
Growth Mindset Avatar answered Oct 02 '22 04:10

Growth Mindset