From f34e8fc890d44f4f913fe812f2cdeb5666f0fa27 Mon Sep 17 00:00:00 2001 From: Anubhav Joshi Date: Sun, 23 Mar 2014 10:40:12 +0530 Subject: [PATCH] Fixed #22257 -- Added file output option to dumpdata command. --- django/core/management/commands/dumpdata.py | 5 ++++- docs/ref/django-admin.txt | 7 +++++++ docs/releases/1.8.txt | 3 ++- tests/fixtures/tests.py | 15 +++++++++++++-- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index 827f2b6c50..045bc96397 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -36,6 +36,8 @@ class Command(BaseCommand): help="Only dump objects with given primary keys. " "Accepts a comma separated list of keys. " "This option will only work when you specify one model."), + make_option('-o' ,'--output', default=None, dest='output', + help='Specifies file to which the output is written.'), ) help = ("Output the contents of the database as a fixture of the given " "format (using each model's default manager unless --all is " @@ -47,6 +49,7 @@ class Command(BaseCommand): indent = options.get('indent') using = options.get('database') excludes = options.get('exclude') + output = options.get('output') show_traceback = options.get('traceback') use_natural_keys = options.get('use_natural_keys') if use_natural_keys: @@ -155,7 +158,7 @@ class Command(BaseCommand): serializers.serialize(format, get_objects(), indent=indent, use_natural_foreign_keys=use_natural_foreign_keys, use_natural_primary_keys=use_natural_primary_keys, - stream=self.stdout) + stream=open(output, 'w') if output else self.stdout) except Exception as e: if show_traceback: raise diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 2daca51382..962d1e196d 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -287,6 +287,13 @@ you can use the ``--pks`` option to specify a comma separated list of primary keys on which to filter. This is only available when dumping one model. +.. versionadded:: 1.8 + +.. django-admin-option:: --output + +By default ``dumpdata`` will output all the serialized data to standard output. +This options allows to specify the file to which the data is to be written. + flush ----- diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 7e81cb6e08..ba17a07cca 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -124,7 +124,8 @@ Internationalization Management Commands ^^^^^^^^^^^^^^^^^^^ -* ... +* :djadmin:`dumpdata` now has the option ``--output`` which allows to specify the +file to which the serialized data is to be written. Models ^^^^^^ diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index fb807bb97c..de976ba666 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import os import warnings from django.contrib.sites.models import Site @@ -37,19 +38,24 @@ class SubclassTestCaseFixtureLoadingTests(TestCaseFixtureLoadingTests): class DumpDataAssertMixin(object): - def _dumpdata_assert(self, args, output, format='json', + def _dumpdata_assert(self, args, output, format='json', filename=None, natural_foreign_keys=False, natural_primary_keys=False, use_base_manager=False, exclude_list=[], primary_keys=''): new_io = six.StringIO() management.call_command('dumpdata', *args, **{'format': format, 'stdout': new_io, 'stderr': new_io, + 'output': filename, 'use_natural_foreign_keys': natural_foreign_keys, 'use_natural_primary_keys': natural_primary_keys, 'use_base_manager': use_base_manager, 'exclude': exclude_list, 'primary_keys': primary_keys}) - command_output = new_io.getvalue().strip() + if filename: + command_output = open(filename, "r").read() + os.remove(filename) + else: + command_output = new_io.getvalue().strip() if format == "json": self.assertJSONEqual(command_output, output) elif format == "xml": @@ -282,6 +288,11 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): primary_keys='2,3' ) + def test_dumpdata_with_file_output(self): + management.call_command('loaddata', 'fixture1.json', verbosity=0) + self._dumpdata_assert(['fixtures'], '[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16T12:00:00"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16T13:00:00"}}, {"pk": 10, "model": "fixtures.book", "fields": {"name": "Achieving self-awareness of Python programs", "authors": []}}]', + filename='dumpdata.json') + def test_compress_format_loading(self): # Load fixture 4 (compressed), using format specification management.call_command('loaddata', 'fixture4.json', verbosity=0)