diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py
index d3650b1..80f3a87 100644
a
|
b
|
from optparse import make_option
|
8 | 8 | |
9 | 9 | class Command(BaseCommand): |
10 | 10 | option_list = BaseCommand.option_list + ( |
| 11 | make_option('-o', '--out', default=None, dest='out', |
| 12 | help='File for dump data. Default to `stdout`'), |
11 | 13 | make_option('--format', default='json', dest='format', |
12 | 14 | help='Specifies the output serialization format for fixtures.'), |
13 | 15 | make_option('--indent', default=None, dest='indent', type='int', |
… |
… |
class Command(BaseCommand):
|
37 | 39 | show_traceback = options.get('traceback') |
38 | 40 | use_natural_keys = options.get('use_natural_keys') |
39 | 41 | use_base_manager = options.get('use_base_manager') |
| 42 | out = options.get('out') |
40 | 43 | |
41 | 44 | excluded_apps = set() |
42 | 45 | excluded_models = set() |
… |
… |
class Command(BaseCommand):
|
111 | 114 | order_by(model._meta.pk.name).iterator(): |
112 | 115 | yield obj |
113 | 116 | |
| 117 | def dump(out): |
| 118 | serializers.serialize(format, get_objects(), indent=indent, |
| 119 | use_natural_keys=use_natural_keys, |
| 120 | stream=out) |
| 121 | |
114 | 122 | try: |
115 | 123 | self.stdout.ending = None |
116 | | serializers.serialize(format, get_objects(), indent=indent, |
117 | | use_natural_keys=use_natural_keys, stream=self.stdout) |
| 124 | if out: |
| 125 | with open(out, 'w') as outfile: |
| 126 | dump(outfile) |
| 127 | else: |
| 128 | dump(self.stdout) |
118 | 129 | except Exception as e: |
119 | 130 | if show_traceback: |
120 | 131 | raise |
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index e0b0845..c3f3413 100644
a
|
b
|
The :djadminopt:`--all` option may be provided to specify that
|
183 | 183 | ``dumpdata`` should use Django's base manager, dumping records which |
184 | 184 | might otherwise be filtered or modified by a custom manager. |
185 | 185 | |
| 186 | .. versionadded:: 1.6 |
| 187 | |
| 188 | .. django-admin-option:: --out <filepath> |
| 189 | |
| 190 | By default, ``dumpdata`` sends dumped data to ``stdout``, but you can save it into file. |
| 191 | If file is exists, it be is rewritten. |
| 192 | |
186 | 193 | .. django-admin-option:: --format <fmt> |
187 | 194 | |
188 | 195 | By default, ``dumpdata`` will format its output in JSON, but you can use the |
diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
index 55363bc..d820268 100644
a
|
b
|
|
2 | 2 | # Unittests for fixtures. |
3 | 3 | from __future__ import absolute_import, unicode_literals |
4 | 4 | |
| 5 | from tempfile import NamedTemporaryFile |
5 | 6 | import os |
6 | 7 | import re |
7 | 8 | |
… |
… |
class TestFixtures(TestCase):
|
362 | 363 | % widget.pk |
363 | 364 | ) |
364 | 365 | |
| 366 | def test_dumpdata_with_to_stdout(self): |
| 367 | stdout = StringIO() |
| 368 | management.call_command( |
| 369 | 'dumpdata', |
| 370 | 'fixtures_regress.widget', |
| 371 | stdout=stdout |
| 372 | ) |
| 373 | self.assertTrue(stdout.getvalue()) |
| 374 | |
| 375 | def test_dumpdata_out_to_file(self): |
| 376 | stdout = StringIO() |
| 377 | with NamedTemporaryFile() as tmpfile: |
| 378 | management.call_command( |
| 379 | 'dumpdata', |
| 380 | 'fixtures_regress.widget', |
| 381 | format='json', |
| 382 | out=tmpfile.name, |
| 383 | stdout=stdout |
| 384 | ) |
| 385 | self.assertFalse(stdout.getvalue()) |
| 386 | self.assertTrue(tmpfile.read()) |
| 387 | |
365 | 388 | def test_loaddata_works_when_fixture_has_forward_refs(self): |
366 | 389 | """ |
367 | 390 | Regression for #3615 - Forward references cause fixtures not to load in MySQL (InnoDB) |