Ticket #16330: dumpdata-primary-keys2.diff

File dumpdata-primary-keys2.diff, 4.0 KB (added by rm_, 4 years ago)

Diffed against latest trunk, coding style cleanup, some code motion in dumpdata to follow how the other options are checked, some rewording of the help text

  • django/core/management/commands/dumpdata.py

     
    55from django.utils.datastructures import SortedDict
    66
    77from optparse import make_option
     8import itertools
     9import sys
    810
    911class Command(BaseCommand):
    1012    option_list = BaseCommand.option_list + (
     
    2123            help='Use natural keys if they are available.'),
    2224        make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False,
    2325            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."),
     26        make_option('--pks', dest='primary_keys', action='append', default=[],
     27            help="Only dump objects with given primary keys. Accepts a space seperated list of keys or '-' to read from stdin. This is mostly useful when dumping only a single model."),
    2428    )
    2529    help = ("Output the contents of the database as a fixture of the given "
    2630            "format (using each model's default manager unless --all is "
     
    3438        indent = options.get('indent')
    3539        using = options.get('database')
    3640        excludes = options.get('exclude')
     41        pks = options.get('primary_keys')
    3742        show_traceback = options.get('traceback')
    3843        use_natural_keys = options.get('use_natural_keys')
    3944        use_base_manager = options.get('use_base_manager')
     
    5459                except ImproperlyConfigured:
    5560                    raise CommandError('Unknown app in excludes: %s' % exclude)
    5661
     62        if pks == ['-']:
     63            # Read from Stdin
     64            primary_keys = sys.stdin.read().split()
     65        else:
     66            primary_keys = list(itertools.chain(*[pk.split() for pk in pks]))
     67
    5768        if len(app_labels) == 0:
    5869            app_list = SortedDict((app, None) for app in get_apps() if app not in excluded_apps)
    5970        else:
     
    104115                continue
    105116            if not model._meta.proxy and router.allow_syncdb(using, model):
    106117                if use_base_manager:
    107                     objects.extend(model._base_manager.using(using).all())
     118                    queryset = model._base_manager.using(using).all()
    108119                else:
    109                     objects.extend(model._default_manager.using(using).all())
     120                    queryset = model._default_manager.using(using).all()
     121                if primary_keys:
     122                    queryset = queryset.filter(pk__in=primary_keys)
     123                objects.extend(queryset)
    110124
    111125        try:
    112126            return serializers.serialize(format, objects, indent=indent,
  • tests/regressiontests/fixtures_regress/tests.py

     
    1616from django.db.models import signals
    1717from django.test import (TestCase, TransactionTestCase, skipIfDBFeature,
    1818    skipUnlessDBFeature)
     19from django.utils import simplejson
    1920
    2021from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget,
    2122    Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3,
     
    341342        self.assertTrue(emu_json in data)
    342343        self.assertTrue(platypus_json in data)
    343344
     345        stdout = StringIO()
     346        management.call_command(
     347            'dumpdata',
     348            'fixtures_regress.animal',
     349            format='json',
     350            stdout=stdout,
     351            primary_keys=['1 10']
     352        )
     353        data = stdout.getvalue()
     354        json = simplejson.loads(data)
     355        self.assertEqual(len(json), 2)
     356        keys = sorted([item['pk'] for item in json])
     357        self.assertEqual(keys, [1, 10])
     358
    344359    def test_proxy_model_included(self):
    345360        """
    346361        Regression for #11428 - Proxy models aren't included when you dumpdata
Back to Top