Ticket #16330: dumpdata-primary-keys3.diff

File dumpdata-primary-keys3.diff, 4.3 KB (added by Bumyong Choi, 12 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..3a1f794 100644
    a b from django.utils.datastructures import SortedDict  
    66
    77from optparse import make_option
    88
     9import itertools
     10import sys
     11
     12
    913class Command(BaseCommand):
    1014    option_list = BaseCommand.option_list + (
    1115        make_option('--format', default='json', dest='format',
    class Command(BaseCommand):  
    2125            help='Use natural keys if they are available.'),
    2226        make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False,
    2327            help="Use Django's base manager to dump all models stored in the database, including those that would otherwise be filtered or modified by a custom manager."),
     28        make_option('--pks', dest='primary_keys', action='append', default=[],
     29            help="Only dump objects with given primary keys. Accepts a space seperated list of keys or '-' to read from stdin. This option is applied to all apps/models."),
    2430    )
    2531    help = ("Output the contents of the database as a fixture of the given "
    2632            "format (using each model's default manager unless --all is "
    class Command(BaseCommand):  
    3440        indent = options.get('indent')
    3541        using = options.get('database')
    3642        excludes = options.get('exclude')
     43        pks = options.get('primary_keys')
    3744        show_traceback = options.get('traceback')
    3845        use_natural_keys = options.get('use_natural_keys')
    3946        use_base_manager = options.get('use_base_manager')
    class Command(BaseCommand):  
    5461                except ImproperlyConfigured:
    5562                    raise CommandError('Unknown app in excludes: %s' % exclude)
    5663
     64        if pks == ['-']:
     65            # Read from Stdin
     66            primary_keys = sys.stdin.read().split()
     67        else:
     68            primary_keys = list(itertools.chain(*[pk.split() for pk in pks]))
     69
    5770        if len(app_labels) == 0:
    5871            app_list = SortedDict((app, None) for app in get_apps() if app not in excluded_apps)
    5972        else:
    class Command(BaseCommand):  
    107120                        objects = model._base_manager
    108121                    else:
    109122                        objects = model._default_manager
    110                     for obj in objects.using(using).\
    111                             order_by(model._meta.pk.name).iterator():
     123
     124                    queryset = objects.using(using).order_by(model._meta.pk.name)
     125                    if primary_keys:
     126                        queryset = queryset.filter(pk__in=primary_keys)
     127
     128                    for obj in queryset.iterator():
    112129                        yield obj
    113130
    114131        try:
    class Command(BaseCommand):  
    120137                raise
    121138            raise CommandError("Unable to serialize database: %s" % e)
    122139
     140
    123141def sort_dependencies(app_list):
    124142    """Sort a list of app,modellist pairs into a single list of models.
    125143
  • tests/regressiontests/fixtures_regress/tests.py

    diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
    index b3d0f42..518107a 100644
    a b from django.db.models import signals  
    1313from django.test import (TestCase, TransactionTestCase, skipIfDBFeature,
    1414    skipUnlessDBFeature)
    1515from django.test.utils import override_settings
     16from django.utils import simplejson
    1617from django.utils.six import PY3, StringIO
    1718
    1819from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget,
    class TestFixtures(TestCase):  
    316317        self.assertTrue(emu_json in data)
    317318        self.assertTrue(platypus_json in data)
    318319
     320        stdout = StringIO()
     321        management.call_command(
     322            'dumpdata',
     323            'fixtures_regress.animal',
     324            format='json',
     325            stdout=stdout,
     326            primary_keys=['1 10']
     327        )
     328        data = stdout.getvalue()
     329        json = simplejson.loads(data)
     330        self.assertEqual(len(json), 2)
     331        keys = sorted([item['pk'] for item in json])
     332        self.assertEqual(keys, [1, 10])
     333
    319334    def test_proxy_model_included(self):
    320335        """
    321336        Regression for #11428 - Proxy models aren't included when you dumpdata
Back to Top