Last active
July 4, 2016 00:29
-
-
Save alexhayes/6d2297ec72be174a1d46df073a55a2d7 to your computer and use it in GitHub Desktop.
Django management command to dump out model instances and all nested associations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Dump out objects and all nested associations. | |
""" | |
from __future__ import absolute_import, print_function, unicode_literals | |
import sys | |
from django.contrib.admin.utils import NestedObjects | |
from django.core import serializers | |
from django.core.management import BaseCommand | |
from django.db import router | |
from django.db.models import Model | |
from django.db.models.loading import get_model | |
class Command(BaseCommand): | |
def add_arguments(self, parser): | |
parser.add_argument('args', metavar='[app_label.ModelName.id1,id2,id3...]', nargs='*', | |
help="Specify object by it's app_label, model and id.") | |
parser.add_argument('--indent', default=None, dest='indent', type=int, | |
help='Specifies the indent level to use when pretty-printing output.') | |
def handle(self, *args, **options): | |
indent = options.get('indent') | |
self.items = [] | |
for arg in args: | |
app_label, model, ids = arg.split('.') | |
Model = get_model(app_label, model) | |
for id in ids.split(','): | |
self.dump_object(Model.objects.filter(pk=id)) | |
serializers.serialize('json', | |
self.items, | |
indent=indent, | |
stream=sys.stdout) | |
def dump_object(self, obj): | |
using = router.db_for_write(obj.__class__) | |
collector = NestedObjects(using=using) | |
collector.collect(obj) | |
objs = collector.nested(lambda x: x) | |
objs = zip(objs[::2], objs[1::2]) | |
self.get_objects(objs) | |
def get_objects(self, item): | |
if isinstance(item, Model): | |
self.items.append(item) | |
elif isinstance(item[0], Model): | |
for _item in item: | |
self.get_objects(_item) | |
else: | |
for obj, children in item: | |
self.items.append(obj) | |
# items.append(obj) | |
self.get_objects(children) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment