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