Fixed #22257 -- Added file output option to dumpdata command.
This commit is contained in:
parent
416a858023
commit
f34e8fc890
|
@ -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
|
||||
|
|
|
@ -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
|
||||
-----
|
||||
|
||||
|
|
|
@ -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
|
||||
^^^^^^
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue