Login

Do Not Escape Characters When Using dumpdata Command (Tested in Django 1.11)

Author:
oldcai
Posted:
October 7, 2017
Language:
Python
Version:
1.10
Score:
0 (after 0 ratings)

Adds --pretty option to django ./manage.py dumpdata command, which produces pretty utf-8 strings instead of ugly unicode-escaped s**t:

$ ./manage.py dumpdata app.pricingplan --indent=1

[
 {
  "pk": 1, 
  "model": "app.pricingplan", 
  "fields": {
   "name": "\u0411\u0430\u0437\u043e\u0432\u044b\u0439", 
  }
 }, 
 {
  "pk": 2, 
  "model": "app.pricingplan", 
  "fields": {
   "name": "\u0425\u0443\u044f\u0437\u043e\u0432\u044b\u0439", 
  }
 }
]

./manage.py dumpdata app.pricingplan --indent=1 --pretty

[
 {
  "pk": 1, 
  "model": "app.pricingplan", 
  "fields": {
   "name": "Базовый", 
  }
 }, 
 {
  "pk": 2, 
  "model": "app.pricingplan", 
  "fields": {
   "name": "Хуязовый", 
  }
 }
]

Forked from an old versions snippet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from io import StringIO
from django.core.management.commands.dumpdata import Command as Dumpdata


class Command(Dumpdata):
    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)
        parser.add_argument(
            '--pretty', default=False, action='store_true',
            dest='pretty', help='Avoid unicode escape symbols'
        )

    def handle(self, *args, **kwargs):
        captcha_stdout = StringIO()
        old_stdout = self.stdout
        self.stdout = captcha_stdout
        super(Command, self).handle(*args, **kwargs)
        captcha_stdout.seek(0)
        data = captcha_stdout.read()
        data = data.encode()
        if kwargs.get('pretty'):
            data = data.decode("unicode_escape").encode("utf-8")
        old_stdout.write(data.decode('utf-8'))

More like this

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

Comments

TedGrassman (on January 4, 2018):

Thanks for this snippet, but if I may: Where do you put this code? I couldn't figure it out... I tried in manage.py with no luck.

#

TedGrassman (on January 4, 2018):

Never mind, I figured it out. See this SO post or this one.

In an existing app in your project or an external reusable app (don't forget to import it in INSTALLED_APPS):

create file myapp/management/commands/dumpdata.py and copy-paste the snippet there :)

#

chamaken (on January 24, 2018):

Thanks, it's useful. How about update to work with -o option:

output = kwargs['output']
kwargs['output'] = None

before call super, then change last line to:

stream = output and open(output, 'w') or old_stdout
stream.write(data.decode('utf-8'))

#

Please login first before commenting.