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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
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.
#
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 :)
#
Thanks, it's useful. How about update to work with -o option:
before call super, then change last line to:
#
Please login first before commenting.