Code

Ticket #16330: dumpdata-primary-keys.patch

File dumpdata-primary-keys.patch, 3.7 KB (added by guettli, 3 years ago)
  • tests/regressiontests/fixtures_regress/tests.py

     
    1515from django.db import transaction 
    1616from django.test import TestCase, TransactionTestCase, skipIfDBFeature, \ 
    1717    skipUnlessDBFeature 
     18from django.utils import simplejson 
    1819 
    1920from models import Animal, Stuff 
    2021from models import Absolute, Parent, Child 
     
    343344        self.assertTrue(emu_json in data) 
    344345        self.assertTrue(platypus_json in data) 
    345346 
     347        stdout = StringIO() 
     348        management.call_command( 
     349            'dumpdata', 
     350            'fixtures_regress.animal', 
     351            format='json', 
     352            stdout=stdout, 
     353            primary_keys=['1 10'], 
     354        ) 
     355        data = stdout.getvalue() 
     356        json=simplejson.loads(data) 
     357        self.assertEqual(len(json), 2) 
     358        keys=sorted([item['pk'] for item in json]) 
     359        self.assertEqual(keys, [1, 10]) 
     360 
    346361    def test_proxy_model_included(self): 
    347362        """ 
    348363        Regression for #11428 - Proxy models aren't included when you dumpdata 
  • django/core/management/commands/dumpdata.py

     
     1import sys 
     2import itertools 
     3 
    14from django.core.exceptions import ImproperlyConfigured 
    25from django.core.management.base import BaseCommand, CommandError 
    36from django.core import serializers 
     
    2124            help='Use natural keys if they are available.'), 
    2225        make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False, 
    2326            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."), 
     27        make_option('--pks', dest='primary_keys', action='append', default=[], 
     28            help="Only dump objects with given primary keys. Accepts a space seperated list of keys or '-' to read from stdin. Normally only usefull if you give a ModelName."), 
    2429    ) 
    2530    help = ("Output the contents of the database as a fixture of the given " 
    2631            "format (using each model's default manager unless --all is " 
     
    3540        using = options.get('database', DEFAULT_DB_ALIAS) 
    3641        connection = connections[using] 
    3742        excludes = options.get('exclude',[]) 
     43        pks=options.get('primary_keys', []) 
     44        assert isinstance(pks, list), pks 
     45        if pks==['-']: 
     46            # Read from Stdin 
     47            primary_keys=sys.stdin.read().split() 
     48        else: 
     49            primary_keys = list(itertools.chain(*[pk.split() for pk in pks])) 
    3850        show_traceback = options.get('traceback', False) 
    3951        use_natural_keys = options.get('use_natural_keys', False) 
    4052        use_base_manager = options.get('use_base_manager', False) 
     
    105117                continue 
    106118            if not model._meta.proxy and router.allow_syncdb(using, model): 
    107119                if use_base_manager: 
    108                     objects.extend(model._base_manager.using(using).all()) 
     120                    queryset=model._base_manager.using(using).all() 
    109121                else: 
    110                     objects.extend(model._default_manager.using(using).all()) 
     122                    queryset=model._default_manager.using(using).all() 
     123                if primary_keys: 
     124                    queryset=queryset.filter(pk__in=primary_keys) 
     125                objects.extend(queryset) 
    111126 
    112127        try: 
    113128            return serializers.serialize(format, objects, indent=indent,