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
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 4 weeks ago
 - get_object_or_none by azwdevops 5 months, 2 weeks ago
 - Mask sensitive data from logger by agusmakmun 7 months, 2 weeks ago
 - Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
 - JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
 
Comments
Is this safe and standards compliant? Why would the Django core devs not have done it the same way?
#
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 )
#
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)
#
This command breaks on e.g. quotes in data because special chars have to be escaped unlike other characters.
From RFC:
#
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.