Login

utf8-friendly dumpdata management command (no escape symbols) #4

Author:
mucius
Posted:
September 8, 2016
Language:
Python
Version:
1.9
Tags:
fixtures management dumpdata
Score:
0 (after 0 ratings)

The version of snippet that works with Django 1.9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
""" pretty serialization
    original from <http://djangosnippets.org/snippets/2397/>
    revised for Django 1.9
"""
import json
from django.core.serializers.json import (  # pylint:disable=W0611
    Serializer as JSONSerializer,
    Deserializer)  # @UnusedImport
from django.core.serializers.json import DjangoJSONEncoder


class Serializer(JSONSerializer):
    """ utf8-friendly dumpdata management command """
    def end_object(self, obj):
        indent = self.options.get("indent")
        if not self.first:
            self.stream.write(",")
            if not indent:
                self.stream.write(" ")
        if indent:
            self.stream.write("\n")
        json.dump(self.get_dump_object(obj), self.stream, ensure_ascii=False,
                  cls=DjangoJSONEncoder, **self.json_kwargs)
        self._current = None  # pylint:disable=W0201

More like this

Comments

Please login first before commenting.