utf8-friendly dumpdata management command (no escape symbols)

 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. streaming serializer by kcarnold 4 years, 1 month ago
  2. utf8-friendly dumpdata management command for yaml (no escape symbols) by mucius 3 days, 17 hours ago
  3. utf8-friendly dumpdata management command (no escape symbols) #2 by kmike 2 years, 2 months ago
  4. utf8-friendly dumpdata management command (no escape symbols) #3 by inductor 2 weeks, 2 days ago
  5. syncdata command by graham 4 years, 9 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)

#

kmike (on March 19, 2011):

Another implementation: http://djangosnippets.org/snippets/2397/

#

(Forgotten your password?)