djommon: add JSON serializer supporting natural primary keys

This commit is contained in:
Benjamin Dauvergne 2014-03-25 15:54:43 +01:00
parent fd48a58cd0
commit ddb023481b
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,37 @@
import json
from django.utils import six
from django.core.serializers.json import Serializer as JSONSerializer
from django.core.serializers.python import Deserializer as \
PythonDeserializer, _get_model
from django.core.serializers.base import DeserializationError
class Serializer(JSONSerializer):
def get_dump_object(self, obj):
d = super(Serializer, self).get_dump_object(obj)
if self.use_natural_keys and hasattr(obj, 'natural_key'):
d['pk'] = obj.natural_key()
return d
def Deserializer(stream_or_string, **options):
"""
Deserialize a stream or string of JSON data.
"""
if not isinstance(stream_or_string, (bytes, six.string_types)):
stream_or_string = stream_or_string.read()
if isinstance(stream_or_string, bytes):
stream_or_string = stream_or_string.decode('utf-8')
try:
objects = json.loads(stream_or_string)
for obj in objects:
Model = _get_model(obj['model'])
if isinstance(obj['pk'], (tuple, list)):
o = Model.objects.get_by_natural_key(*obj['pk'])
obj['pk'] = o.pk
for obj in PythonDeserializer(objects, **options):
yield obj
except GeneratorExit:
raise
except Exception as e:
# Map to deserializer error
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])