Login

utf8-friendly dumpdata management command (no escape symbols)

Author:
dirol
Posted:
November 12, 2010
Language:
Python
Version:
1.2
Score:
4 (after 4 ratings)

Adds --pretty option to django ./manage.py dumpdata command, which produces pretty utf8 strings instead of ugly unicode-escaped shit:

$ ./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": "Хуязовый", 
  }
 }
]%
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/usr/bin/env python
from optparse import make_option

from django.core.management.commands.dumpdata import Command as Dumpdata

class Command(Dumpdata):
    option_list = Dumpdata.option_list + (
        make_option('--pretty', default=False, action='store_true', 
            dest='pretty', help='Avoid unicode escape symbols'
        ),
    )
    
    def handle(self, *args, **kwargs):
        data = super(Command, self).handle(*args, **kwargs)
        if kwargs.get('pretty'):
            data = data.decode("unicode_escape").encode("utf8")
        return data

More like this

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

Comments

eallik (on November 12, 2010):

Is this safe and standards compliant? Why would the Django core devs not have done it the same way?

#

dirol (on November 12, 2010):

I don't know whether it is safe or not, but fixture generated by this command: 1. Are easy-editable 2. Successfully loads back to database with ./manage.py loaddata.

The rest i leave to you )

#

dirol (on November 12, 2010):

And oh, I only tested json fixtures. I have no idea if it works fine with yaml, and I'm pretty sure It's useless with xml (cause xml fixtures are already fine)

#

kmike (on March 19, 2011):

This command breaks on e.g. quotes in data because special chars have to be escaped unlike other characters.

From RFC:

All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F)

#

oldcai (on October 7, 2017):

I've written a new snippet for this issue, which is suitable for Django 1.11(and maybe later), see it here

#

Please login first before commenting.