From f5412b781865a16e5f203f8330f28dfa509110cf Mon Sep 17 00:00:00 2001 From: miigotu Date: Mon, 14 Nov 2016 06:01:21 -0800 Subject: [PATCH] dump_object: Add support for --format, fix kitchensink for django >= 1.8 Signed-off-by: miigotu --- .../management/commands/dump_object.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/fixture_magic/management/commands/dump_object.py b/fixture_magic/management/commands/dump_object.py index ad1d123..862a751 100644 --- a/fixture_magic/management/commands/dump_object.py +++ b/fixture_magic/management/commands/dump_object.py @@ -1,8 +1,8 @@ from __future__ import print_function from optparse import make_option -from past.builtins import long +import django from django.core.exceptions import FieldError, ObjectDoesNotExist from django.core.management.base import BaseCommand, CommandError from django.core.serializers import serialize @@ -45,6 +45,11 @@ def add_arguments(self, parser): help=('Use a json query rather than list of ids ' 'e.g. \'{\"pk__in\": [id, ...]}\'')) + parser.add_argument( + '--format', default='json', dest='format', + help='Specifies the output serialization format for fixtures.', + ) + def handle(self, *args, **options): error_text = ('%s\nTry calling dump_object with --help argument or ' + 'use the following arguments:\n %s' % self.args) @@ -88,8 +93,14 @@ def handle(self, *args, **options): break if options.get('kitchensink'): + if django.VERSION >= (1, 8): + fields = (f for f in dump_me._meta.get_fields() + if (f.one_to_many or f.one_to_one) and f.auto_created) + else: + fields = dump_me._meta.get_all_related_objects() + related_fields = [rel.get_accessor_name() for rel in - dump_me._meta.get_all_related_objects()] + fields] for obj in objs: for rel in related_fields: @@ -105,7 +116,7 @@ def handle(self, *args, **options): add_to_serialize_list(objs) serialize_fully() - self.stdout.write(serialize('json', [o for o in serialize_me if o is not None], + self.stdout.write(serialize(options.get('format','json'), [o for o in serialize_me if o is not None], indent=4, use_natural_foreign_keys=options.get('natural', False), use_natural_primary_keys=options.get('natural', False)))