Ticket #16330: dumpdata-primary-keys2.diff
File dumpdata-primary-keys2.diff, 4.0 KB (added by , 13 years ago) |
---|
-
django/core/management/commands/dumpdata.py
5 5 from django.utils.datastructures import SortedDict 6 6 7 7 from optparse import make_option 8 import itertools 9 import sys 8 10 9 11 class Command(BaseCommand): 10 12 option_list = BaseCommand.option_list + ( … … 21 23 help='Use natural keys if they are available.'), 22 24 make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False, 23 25 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."), 24 28 ) 25 29 help = ("Output the contents of the database as a fixture of the given " 26 30 "format (using each model's default manager unless --all is " … … 34 38 indent = options.get('indent') 35 39 using = options.get('database') 36 40 excludes = options.get('exclude') 41 pks = options.get('primary_keys') 37 42 show_traceback = options.get('traceback') 38 43 use_natural_keys = options.get('use_natural_keys') 39 44 use_base_manager = options.get('use_base_manager') … … 54 59 except ImproperlyConfigured: 55 60 raise CommandError('Unknown app in excludes: %s' % exclude) 56 61 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 57 68 if len(app_labels) == 0: 58 69 app_list = SortedDict((app, None) for app in get_apps() if app not in excluded_apps) 59 70 else: … … 104 115 continue 105 116 if not model._meta.proxy and router.allow_syncdb(using, model): 106 117 if use_base_manager: 107 objects.extend(model._base_manager.using(using).all())118 queryset = model._base_manager.using(using).all() 108 119 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) 110 124 111 125 try: 112 126 return serializers.serialize(format, objects, indent=indent, -
tests/regressiontests/fixtures_regress/tests.py
16 16 from django.db.models import signals 17 17 from django.test import (TestCase, TransactionTestCase, skipIfDBFeature, 18 18 skipUnlessDBFeature) 19 from django.utils import simplejson 19 20 20 21 from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget, 21 22 Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3, … … 341 342 self.assertTrue(emu_json in data) 342 343 self.assertTrue(platypus_json in data) 343 344 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 344 359 def test_proxy_model_included(self): 345 360 """ 346 361 Regression for #11428 - Proxy models aren't included when you dumpdata