summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorPreston Holmes <preston@ptone.com>2013-05-19 12:39:14 +0200
committerPreston Holmes <preston@ptone.com>2013-05-19 09:10:40 -0700
commit6786920fd8a1dfa43bba8333548c2496847298af (patch)
tree572ca38edf73ec30095bd25f691b6879b230dfc1 /django
parentbdde7feb26f89e580c2b21154196fe3f8c774677 (diff)
Fixed #16330 -- added --pks option in dumpdata command
Thanks to guettli for the initial ticket and patch, with additional work from mehmetakyuz and Kevin Brolly.
Diffstat (limited to 'django')
-rw-r--r--django/core/management/commands/dumpdata.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py
index d3650b1eb8..b1e06e4255 100644
--- a/django/core/management/commands/dumpdata.py
+++ b/django/core/management/commands/dumpdata.py
@@ -21,6 +21,8 @@ class Command(BaseCommand):
help='Use natural keys if they are available.'),
make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False,
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."),
+ make_option('--pks', dest='primary_keys', action='append', default=[],
+ help="Only dump objects with given primary keys. Accepts a comma seperated list of keys. This option will only work when you specify one model."),
)
help = ("Output the contents of the database as a fixture of the given "
"format (using each model's default manager unless --all is "
@@ -37,6 +39,12 @@ class Command(BaseCommand):
show_traceback = options.get('traceback')
use_natural_keys = options.get('use_natural_keys')
use_base_manager = options.get('use_base_manager')
+ pks = options.get('primary_keys')
+
+ if pks:
+ primary_keys = pks.split(',')
+ else:
+ primary_keys = False
excluded_apps = set()
excluded_models = set()
@@ -55,8 +63,12 @@ class Command(BaseCommand):
raise CommandError('Unknown app in excludes: %s' % exclude)
if len(app_labels) == 0:
+ if primary_keys:
+ raise CommandError("You can only use --pks option with one model")
app_list = SortedDict((app, None) for app in get_apps() if app not in excluded_apps)
else:
+ if len(app_labels) > 1 and primary_keys:
+ raise CommandError("You can only use --pks option with one model")
app_list = SortedDict()
for label in app_labels:
try:
@@ -77,6 +89,8 @@ class Command(BaseCommand):
else:
app_list[app] = [model]
except ValueError:
+ if primary_keys:
+ raise CommandError("You can only use --pks option with one model")
# This is just an app - no model qualifier
app_label = label
try:
@@ -107,8 +121,11 @@ class Command(BaseCommand):
objects = model._base_manager
else:
objects = model._default_manager
- for obj in objects.using(using).\
- order_by(model._meta.pk.name).iterator():
+
+ queryset = objects.using(using).order_by(model._meta.pk.name)
+ if primary_keys:
+ queryset = queryset.filter(pk__in=primary_keys)
+ for obj in queryset.iterator():
yield obj
try: