1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

Fixed #5610 -- Added the ability for dumpdata to take individual model names, as well as entire applications. Thanks to David Reynolds for his work on this patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9921 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee
2009-02-28 05:35:22 +00:00
parent b337884fcf
commit db2a95f6f5
4 changed files with 113 additions and 16 deletions

View File

@@ -1,3 +1,4 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError
from django.core import serializers
@@ -9,14 +10,14 @@ class Command(BaseCommand):
help='Specifies the output serialization format for fixtures.'),
make_option('--indent', default=None, dest='indent', type='int',
help='Specifies the indent level to use when pretty-printing output'),
make_option('-e', '--exclude', dest='exclude',action='append', default=[],
make_option('-e', '--exclude', dest='exclude',action='append', default=[],
help='App to exclude (use multiple --exclude to exclude multiple apps).'),
)
help = 'Output the contents of the database as a fixture of the given format.'
args = '[appname ...]'
def handle(self, *app_labels, **options):
from django.db.models import get_app, get_apps, get_models
from django.db.models import get_app, get_apps, get_models, get_model
format = options.get('format','json')
indent = options.get('indent',None)
@@ -26,9 +27,34 @@ class Command(BaseCommand):
excluded_apps = [get_app(app_label) for app_label in exclude]
if len(app_labels) == 0:
app_list = [app for app in get_apps() if app not in excluded_apps]
app_list = dict([(app, None) for app in get_apps() if app not in excluded_apps])
else:
app_list = [get_app(app_label) for app_label in app_labels]
app_list = {}
for label in app_labels:
try:
app_label, model_label = label.split('.')
try:
app = get_app(app_label)
except ImproperlyConfigured:
raise CommandError("Unknown application: %s" % app_label)
model = get_model(app_label, model_label)
if model is None:
raise CommandError("Unknown model: %s.%s" % (app_label, model_label))
if app in app_list.keys():
if app_list[app] and model not in app_list[app]:
app_list[app].append(model)
else:
app_list[app] = [model]
except ValueError:
# This is just an app - no model qualifier
app_label = label
try:
app = get_app(app_label)
except ImproperlyConfigured:
raise CommandError("Unknown application: %s" % app_label)
app_list[app] = None
# Check that the serialization format exists; this is a shortcut to
# avoid collating all the objects and _then_ failing.
@@ -41,9 +67,13 @@ class Command(BaseCommand):
raise CommandError("Unknown serialization format: %s" % format)
objects = []
for app in app_list:
for model in get_models(app):
objects.extend(model._default_manager.all())
for app, model_list in app_list.items():
if model_list is None:
model_list = get_models(app)
for model in model_list:
objects.extend(model.objects.all())
try:
return serializers.serialize(format, objects, indent=indent)
except Exception, e: