From db2a95f6f58f5593dbcee8dcf51f3e9d71449708 Mon Sep 17 00:00:00 2001
From: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat, 28 Feb 2009 05:35:22 +0000
Subject: [PATCH] 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
---
 django/core/management/commands/dumpdata.py   | 44 +++++++++++++++---
 docs/ref/django-admin.txt                     | 10 ++++-
 .../fixtures/fixtures/fixture1.json           | 30 ++++++++++---
 tests/modeltests/fixtures/models.py           | 45 ++++++++++++++++++-
 4 files changed, 113 insertions(+), 16 deletions(-)

diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py
index 2559d57104..eecf9404f3 100644
--- a/django/core/management/commands/dumpdata.py
+++ b/django/core/management/commands/dumpdata.py
@@ -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:
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index 7b6b6c4fd6..dff5e681f8 100644
--- a/docs/ref/django-admin.txt
+++ b/docs/ref/django-admin.txt
@@ -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
 -----
 
diff --git a/tests/modeltests/fixtures/fixtures/fixture1.json b/tests/modeltests/fixtures/fixtures/fixture1.json
index cc11a3e926..332feaef77 100644
--- a/tests/modeltests/fixtures/fixtures/fixture1.json
+++ b/tests/modeltests/fixtures/fixtures/fixture1.json
@@ -1,18 +1,34 @@
 [
     {
-        "pk": "2", 
-        "model": "fixtures.article", 
+        "pk": 1,
+        "model": "sites.site",
         "fields": {
-            "headline": "Poker has no place on ESPN", 
+            "domain": "example.com",
+            "name": "example.com"
+        }
+    },
+    {
+        "pk": "2",
+        "model": "fixtures.article",
+        "fields": {
+            "headline": "Poker has no place on ESPN",
             "pub_date": "2006-06-16 12:00:00"
         }
-    }, 
+    },
     {
-        "pk": "3", 
-        "model": "fixtures.article", 
+        "pk": "3",
+        "model": "fixtures.article",
         "fields": {
-            "headline": "Time to reform copyright", 
+            "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
diff --git a/tests/modeltests/fixtures/models.py b/tests/modeltests/fixtures/models.py
index 0a32962860..75ff99867a 100644
--- a/tests/modeltests/fixtures/models.py
+++ b/tests/modeltests/fixtures/models.py
@@ -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