Commit db2a95f6 authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

Fixed #5610 -- Added the ability for dumpdata to take individual model names,...

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
parent b337884f
Loading
Loading
Loading
Loading
+37 −7
Original line number Diff line number Diff line
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError
from django.core import serializers

@@ -16,7 +17,7 @@ class Command(BaseCommand):
    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:
+9 −1
Original line number Diff line number Diff line
@@ -186,7 +186,7 @@ if you're ever curious to see the full list of defaults.
dumpdata
--------

.. django-admin:: dumpdata <appname appname ...>
.. django-admin:: dumpdata <appname appname appname.Model ...>

Outputs to standard output all data in the database associated with the named
application(s).
@@ -228,6 +228,14 @@ directives::
    easy for humans to read, so you can use the ``--indent`` option to
    pretty-print the output with a number of indentation spaces.

.. versionadded: 1.1

In addition to specifying application names, you can provide a list of
individual models, in the form of ``appname.Model``. If you specify a model
name to ``dumpdata``, the dumped output will be restricted to that model,
rather than the entire application. You can also mix application names and
model names.

flush
-----

+23 −7
Original line number Diff line number Diff line
[
    {
        "pk": 1,
        "model": "sites.site",
        "fields": {
            "domain": "example.com",
            "name": "example.com"
        }
    },
    {
        "pk": "2",
        "model": "fixtures.article",
@@ -14,5 +22,13 @@
            "headline": "Time to reform copyright",
            "pub_date": "2006-06-16 13:00:00"
        }
    },
    {
        "pk": 1,
        "model": "fixtures.category",
        "fields": {
            "description": "Latest news stories",
            "title": "News Stories"
        }
    }
]
 No newline at end of file
+44 −1
Original line number Diff line number Diff line
@@ -11,6 +11,16 @@ in the application directory, on in one of the directories named in the
from django.db import models
from django.conf import settings

class Category(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ('title',)

class Article(models.Model):
    headline = models.CharField(max_length=100, default='Default headline')
    pub_date = models.DateTimeField()
@@ -38,6 +48,38 @@ __test__ = {'API_TESTS': """
>>> Article.objects.all()
[<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]

# Dump the current contents of the database as a JSON fixture
>>> management.call_command('dumpdata', 'fixtures', format='json')
[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]

# Try just dumping the contents of fixtures.Category
>>> management.call_command('dumpdata', 'fixtures.Category', format='json')
[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}]

# ...and just fixtures.Article
>>> management.call_command('dumpdata', 'fixtures.Article', format='json')
[{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]

# ...and both
>>> management.call_command('dumpdata', 'fixtures.Category', 'fixtures.Article', format='json')
[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]

# Specify a specific model twice
>>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures.Article', format='json')
[{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]

# Specify a dump that specifies Article both explicitly and implicitly
>>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures', format='json')
[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]

# Same again, but specify in the reverse order
>>> management.call_command('dumpdata', 'fixtures', 'fixtures.Article', format='json')
[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]

# Specify one model from one application, and an entire other application.
>>> management.call_command('dumpdata', 'fixtures.Category', 'sites', format='json')
[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}]

# Load fixture 2. JSON file imported by default. Overwrites some existing objects
>>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
>>> Article.objects.all()
@@ -82,7 +124,7 @@ Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.

# Dump the current contents of the database as a JSON fixture
>>> management.call_command('dumpdata', 'fixtures', format='json')
[{"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]
[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 3, "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": 2, "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": 1, "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}]

# Load fixture 4 (compressed), using format discovery
>>> management.call_command('loaddata', 'fixture4', verbosity=0)
@@ -116,6 +158,7 @@ Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.
# because there are two fixture5's in the fixtures directory
>>> management.call_command('loaddata', 'fixture5', verbosity=0) # doctest: +ELLIPSIS
Multiple fixtures named 'fixture5' in '...fixtures'. Aborting.

"""

from django.test import TestCase