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
- 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, 3 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
"cls=DjangoJSONEncoder" should be removed in Django 2.0.
#
Please login first before commenting.