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
|
6 | 6 | |
7 | 7 | from optparse import make_option |
8 | 8 | |
| 9 | import itertools |
| 10 | import sys |
| 11 | |
| 12 | |
9 | 13 | class Command(BaseCommand): |
10 | 14 | option_list = BaseCommand.option_list + ( |
11 | 15 | make_option('--format', default='json', dest='format', |
… |
… |
class Command(BaseCommand):
|
21 | 25 | help='Use natural keys if they are available.'), |
22 | 26 | make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False, |
23 | 27 | 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."), |
24 | 30 | ) |
25 | 31 | help = ("Output the contents of the database as a fixture of the given " |
26 | 32 | "format (using each model's default manager unless --all is " |
… |
… |
class Command(BaseCommand):
|
34 | 40 | indent = options.get('indent') |
35 | 41 | using = options.get('database') |
36 | 42 | excludes = options.get('exclude') |
| 43 | pks = options.get('primary_keys') |
37 | 44 | show_traceback = options.get('traceback') |
38 | 45 | use_natural_keys = options.get('use_natural_keys') |
39 | 46 | use_base_manager = options.get('use_base_manager') |
… |
… |
class Command(BaseCommand):
|
54 | 61 | except ImproperlyConfigured: |
55 | 62 | raise CommandError('Unknown app in excludes: %s' % exclude) |
56 | 63 | |
| 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 | |
57 | 70 | if len(app_labels) == 0: |
58 | 71 | app_list = SortedDict((app, None) for app in get_apps() if app not in excluded_apps) |
59 | 72 | else: |
… |
… |
class Command(BaseCommand):
|
107 | 120 | objects = model._base_manager |
108 | 121 | else: |
109 | 122 | 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(): |
112 | 129 | yield obj |
113 | 130 | |
114 | 131 | try: |
… |
… |
class Command(BaseCommand):
|
120 | 137 | raise |
121 | 138 | raise CommandError("Unable to serialize database: %s" % e) |
122 | 139 | |
| 140 | |
123 | 141 | def sort_dependencies(app_list): |
124 | 142 | """Sort a list of app,modellist pairs into a single list of models. |
125 | 143 | |
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
|
13 | 13 | from django.test import (TestCase, TransactionTestCase, skipIfDBFeature, |
14 | 14 | skipUnlessDBFeature) |
15 | 15 | from django.test.utils import override_settings |
| 16 | from django.utils import simplejson |
16 | 17 | from django.utils.six import PY3, StringIO |
17 | 18 | |
18 | 19 | from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget, |
… |
… |
class TestFixtures(TestCase):
|
316 | 317 | self.assertTrue(emu_json in data) |
317 | 318 | self.assertTrue(platypus_json in data) |
318 | 319 | |
| 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 | |
319 | 334 | def test_proxy_model_included(self): |
320 | 335 | """ |
321 | 336 | Regression for #11428 - Proxy models aren't included when you dumpdata |