Ticket #19115: 19115.patch

File 19115.patch, 3.8 KB (added by Roman Gladkov, 11 years ago)
  • django/core/management/commands/dumpdata.py

    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  
    88
    99class Command(BaseCommand):
    1010    option_list = BaseCommand.option_list + (
     11        make_option('-o', '--out', default=None, dest='out',
     12                    help='File for dump data. Default to `stdout`'),
    1113        make_option('--format', default='json', dest='format',
    1214            help='Specifies the output serialization format for fixtures.'),
    1315        make_option('--indent', default=None, dest='indent', type='int',
    class Command(BaseCommand):  
    3739        show_traceback = options.get('traceback')
    3840        use_natural_keys = options.get('use_natural_keys')
    3941        use_base_manager = options.get('use_base_manager')
     42        out = options.get('out')
    4043
    4144        excluded_apps = set()
    4245        excluded_models = set()
    class Command(BaseCommand):  
    111114                            order_by(model._meta.pk.name).iterator():
    112115                        yield obj
    113116
     117        def dump(out):
     118            serializers.serialize(format, get_objects(), indent=indent,
     119                                  use_natural_keys=use_natural_keys,
     120                                  stream=out)
     121
    114122        try:
    115123            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)
    118129        except Exception as e:
    119130            if show_traceback:
    120131                raise
  • docs/ref/django-admin.txt

    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  
    183183``dumpdata`` should use Django's base manager, dumping records which
    184184might otherwise be filtered or modified by a custom manager.
    185185
     186.. versionadded:: 1.6
     187
     188.. django-admin-option:: --out <filepath>
     189
     190By default, ``dumpdata`` sends dumped data to ``stdout``, but you can save it into file.
     191If file is exists, it be is rewritten.
     192
    186193.. django-admin-option:: --format <fmt>
    187194
    188195By default, ``dumpdata`` will format its output in JSON, but you can use the
  • tests/regressiontests/fixtures_regress/tests.py

    diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
    index 55363bc..d820268 100644
    a b  
    22# Unittests for fixtures.
    33from __future__ import absolute_import, unicode_literals
    44
     5from tempfile import NamedTemporaryFile
    56import os
    67import re
    78
    class TestFixtures(TestCase):  
    362363            % widget.pk
    363364            )
    364365
     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
    365388    def test_loaddata_works_when_fixture_has_forward_refs(self):
    366389        """
    367390        Regression for #3615 - Forward references cause fixtures not to load in MySQL (InnoDB)
Back to Top