Skip to content

Commit

Permalink
dump_object: Add support for --format, fix kitchensink for django >= 1.8
Browse files Browse the repository at this point in the history
Signed-off-by: miigotu <[email protected]>
  • Loading branch information
miigotu committed Nov 14, 2016
1 parent aae71d2 commit f5412b7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions fixture_magic/management/commands/dump_object.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)))
Expand Down

0 comments on commit f5412b7

Please sign in to comment.