Django JSONField dumping / loading

I use JSONField in some of my Django models and want to transfer this data from Oracle to Postgres.

So far, I have not been able to save JSON data unchanged when using Django dumpdata and loaddata commands, the data is converted to JSON string representations. I have yet to find a good solution for this ... Ideas?

+7
source share
3 answers

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 # 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 (ie, 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 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.

+6
source

I have not used JSONField before, but I do this:

 import json data_structure = json.loads(myData) 

Perhaps this will work for what you need. Probably the best way to handle this.

0
source

EDIT: if you finish using the json package - only then the following solution applies.

If you are using Python 2.6 and above, you can use:

 import json 

Otherwise, you can use simplejson, which is associated with django.utils (for Python <2.6).

 from django.utils import simplejson as json 

Thus, you can continue to use the same package name and transfer your code to the Google App Engine, since it supports Python 2.5.2 at the moment.

0
source

All Articles