Login

utf8-friendly dumpdata management command (no escape symbols)

Author:
dirol
Posted:
November 12, 2010
Language:
Python
Version:
1.2
Tags:
fixtures management dumpdata
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. utf8-friendly dumpdata management command (no escape symbols) #2 by kmike 3 years, 11 months ago
  2. utf8-friendly dumpdata management command for yaml (no escape symbols) by mucius 1 year, 9 months ago
  3. Command to make fixtures. by buriy 6 years, 7 months ago
  4. utf8-friendly dumpdata management command (no escape symbols) #3 by inductor 1 year, 9 months ago
  5. Template Filter: Add indentation by chuck-harmston 5 years, 6 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)

#

Please login first before commenting.