Ticket #16330: dumpdata-primary-keys.patch
File dumpdata-primary-keys.patch, 3.7 KB (added by , 13 years ago) |
---|
-
tests/regressiontests/fixtures_regress/tests.py
15 15 from django.db import transaction 16 16 from django.test import TestCase, TransactionTestCase, skipIfDBFeature, \ 17 17 skipUnlessDBFeature 18 from django.utils import simplejson 18 19 19 20 from models import Animal, Stuff 20 21 from models import Absolute, Parent, Child … … 343 344 self.assertTrue(emu_json in data) 344 345 self.assertTrue(platypus_json in data) 345 346 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 346 361 def test_proxy_model_included(self): 347 362 """ 348 363 Regression for #11428 - Proxy models aren't included when you dumpdata -
django/core/management/commands/dumpdata.py
1 import sys 2 import itertools 3 1 4 from django.core.exceptions import ImproperlyConfigured 2 5 from django.core.management.base import BaseCommand, CommandError 3 6 from django.core import serializers … … 21 24 help='Use natural keys if they are available.'), 22 25 make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False, 23 26 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."), 24 29 ) 25 30 help = ("Output the contents of the database as a fixture of the given " 26 31 "format (using each model's default manager unless --all is " … … 35 40 using = options.get('database', DEFAULT_DB_ALIAS) 36 41 connection = connections[using] 37 42 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])) 38 50 show_traceback = options.get('traceback', False) 39 51 use_natural_keys = options.get('use_natural_keys', False) 40 52 use_base_manager = options.get('use_base_manager', False) … … 105 117 continue 106 118 if not model._meta.proxy and router.allow_syncdb(using, model): 107 119 if use_base_manager: 108 objects.extend(model._base_manager.using(using).all())120 queryset=model._base_manager.using(using).all() 109 121 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) 111 126 112 127 try: 113 128 return serializers.serialize(format, objects, indent=indent,