Login

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

Author:
mucius
Posted:
September 8, 2016
Language:
Python
Version:
1.9
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

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

mucius (on October 31, 2017):

"cls=DjangoJSONEncoder" should be removed in Django 2.0.

#

Please login first before commenting.