Merge pull request #2465 from coder9042/ticket_22258
Fixed #22257 -- Added file output option to dumpdata command.
This commit is contained in:
commit
09ab447d08
|
@ -36,6 +36,8 @@ class Command(BaseCommand):
|
||||||
help="Only dump objects with given primary keys. "
|
help="Only dump objects with given primary keys. "
|
||||||
"Accepts a comma separated list of keys. "
|
"Accepts a comma separated list of keys. "
|
||||||
"This option will only work when you specify one model."),
|
"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 "
|
help = ("Output the contents of the database as a fixture of the given "
|
||||||
"format (using each model's default manager unless --all is "
|
"format (using each model's default manager unless --all is "
|
||||||
|
@ -47,6 +49,7 @@ class Command(BaseCommand):
|
||||||
indent = options.get('indent')
|
indent = options.get('indent')
|
||||||
using = options.get('database')
|
using = options.get('database')
|
||||||
excludes = options.get('exclude')
|
excludes = options.get('exclude')
|
||||||
|
output = options.get('output')
|
||||||
show_traceback = options.get('traceback')
|
show_traceback = options.get('traceback')
|
||||||
use_natural_keys = options.get('use_natural_keys')
|
use_natural_keys = options.get('use_natural_keys')
|
||||||
if use_natural_keys:
|
if use_natural_keys:
|
||||||
|
@ -155,7 +158,7 @@ class Command(BaseCommand):
|
||||||
serializers.serialize(format, get_objects(), indent=indent,
|
serializers.serialize(format, get_objects(), indent=indent,
|
||||||
use_natural_foreign_keys=use_natural_foreign_keys,
|
use_natural_foreign_keys=use_natural_foreign_keys,
|
||||||
use_natural_primary_keys=use_natural_primary_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:
|
except Exception as e:
|
||||||
if show_traceback:
|
if show_traceback:
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -277,6 +277,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
|
primary keys on which to filter. This is only available when dumping
|
||||||
one model.
|
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
|
flush
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
|
@ -124,7 +124,8 @@ Internationalization
|
||||||
Management Commands
|
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
|
Models
|
||||||
^^^^^^
|
^^^^^^
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import os
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
|
@ -37,18 +38,23 @@ class SubclassTestCaseFixtureLoadingTests(TestCaseFixtureLoadingTests):
|
||||||
|
|
||||||
class DumpDataAssertMixin(object):
|
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,
|
natural_foreign_keys=False, natural_primary_keys=False,
|
||||||
use_base_manager=False, exclude_list=[], primary_keys=''):
|
use_base_manager=False, exclude_list=[], primary_keys=''):
|
||||||
new_io = six.StringIO()
|
new_io = six.StringIO()
|
||||||
management.call_command('dumpdata', *args, **{'format': format,
|
management.call_command('dumpdata', *args, **{'format': format,
|
||||||
'stdout': new_io,
|
'stdout': new_io,
|
||||||
'stderr': new_io,
|
'stderr': new_io,
|
||||||
|
'output': filename,
|
||||||
'use_natural_foreign_keys': natural_foreign_keys,
|
'use_natural_foreign_keys': natural_foreign_keys,
|
||||||
'use_natural_primary_keys': natural_primary_keys,
|
'use_natural_primary_keys': natural_primary_keys,
|
||||||
'use_base_manager': use_base_manager,
|
'use_base_manager': use_base_manager,
|
||||||
'exclude': exclude_list,
|
'exclude': exclude_list,
|
||||||
'primary_keys': primary_keys})
|
'primary_keys': primary_keys})
|
||||||
|
if filename:
|
||||||
|
command_output = open(filename, "r").read()
|
||||||
|
os.remove(filename)
|
||||||
|
else:
|
||||||
command_output = new_io.getvalue().strip()
|
command_output = new_io.getvalue().strip()
|
||||||
if format == "json":
|
if format == "json":
|
||||||
self.assertJSONEqual(command_output, output)
|
self.assertJSONEqual(command_output, output)
|
||||||
|
@ -282,6 +288,11 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase):
|
||||||
primary_keys='2,3'
|
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):
|
def test_compress_format_loading(self):
|
||||||
# Load fixture 4 (compressed), using format specification
|
# Load fixture 4 (compressed), using format specification
|
||||||
management.call_command('loaddata', 'fixture4.json', verbosity=0)
|
management.call_command('loaddata', 'fixture4.json', verbosity=0)
|
||||||
|
|
Loading…
Reference in New Issue