This is a revised version of https://djangosnippets.org/snippets/2921/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | """ pretty serialization
original from <http://djangosnippets.org/snippets/2397/>
"""
import sys
from io import StringIO
import datetime
import yaml
try:
from yaml import CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader
import pytz
from django.core.serializers.base import DeserializationError
from django.utils import six
from django.core.serializers.pyyaml import (
Serializer as YamlSerializer, DjangoSafeDumper)
from django.core.serializers.python import (
Deserializer as PythonDeserializer,
)
class Serializer(YamlSerializer):
""" utf8-friendly dumpdata management command """
def end_serialization(self):
yaml.dump(self.objects, self.stream, allow_unicode=True,
default_flow_style=False,
Dumper=DjangoSafeDumper, **self.options)
def Deserializer(stream_or_string, **options): # pylint:disable=C0103
"""
Deserialize a stream or string of YAML data.
"""
if isinstance(stream_or_string, bytes):
stream_or_string = stream_or_string.decode('utf-8')
if isinstance(stream_or_string, six.string_types):
stream = StringIO(stream_or_string)
else:
stream = stream_or_string
try: # pylint:disable=R0101
output = yaml.load(stream, Loader=SafeLoader)
for a_model in output:
for key, value in a_model.items():
if key == 'fields':
for vkey, vvalue in value.items():
if isinstance(vvalue, datetime.datetime):
value[vkey] = vvalue.replace(tzinfo=pytz.utc)
for obj in PythonDeserializer(output, **options):
yield obj
except GeneratorExit:
raise
except Exception as except_info: # pylint:disable=W0703
# Map to deserializer error
six.reraise(
DeserializationError, DeserializationError(
except_info), sys.exc_info()[2])
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.