开发者

Django JSONField dumping/loading

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:

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.


I haven't used the JSONField before, but what I do is:

import json

data_structure = json.loads(myData)

Maybe that will work for what you need as well. There's likely a better way to deal with this.


EDIT: If you end up using the package json - only then is the following solution applicable.

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

import json

otherwise, you can use the simplejson that is bundled with django.utils (for Python < 2.6).

from django.utils import simplejson as json

That way you can continue to use the same package name, and take your code to Google App Engine as it supports Python 2.5.2 at the moment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜