Alternative to django's dumpdata command

The json output by manage.py dumpdata of django was not good because Japanese was escaped and the field names were out of order of the model, so I used interactive instead. (The model name is different from the actual one for example)

>>> import json
>>> from collections import OrderedDict
>>> from django.core.serializers.json import DjangoJSONEncoder
>>> from account.models import User
>>> users = list(User.objects.all().order_by('id'))
>>> field_names = ["name", "birthday", "description"]
>>> a = [OrderedDict([("pk", user.pk), ("model", "account.user"), ("fields", OrderedDict([(name, getattr(user, name)) for name in field_names]))]) for user in users]
>>> print json.dumps(a, cls=DjangoJSONEncoder, indent=2)

The point is

--OrderedDict is json.dump and the order is maintained --cls = DjangoJSONEncoder is required to output datetime

Around.

Since the field name should be taken from the attribute of the model, it seems that it can be made more general.

(Addition)

Japanese is escaped

After all, this problem is solved ad hoc by pasting the output once into the Python 3 shell.

Recommended Posts

Alternative to django's dumpdata command
Poetry-An alternative to Pipenv
Command to generate QR code
How to use Django's GeoIp2
[systemd] Command to delete service
Introduction to vi command (memorandum)
Command to create Linux Live USB
In the python command python points to python3.8
How to use MBDyn (command setting)
[linux] kill command to kill the process
Run scripts with Django's admin command
[Django] Command to output QuerySet to csv
Add parameters to Django's custom commands
An alternative to `pause` in Python