Author: russellm
Date: 2009-02-27 23:35:22 -0600 (Fri, 27 Feb 2009)
New Revision: 9921
Modified:
django/trunk/django/core/management/commands/dumpdata.py
django/trunk/docs/ref/django-admin.txt
django/trunk/tests/modeltests/fixtures/fixtures/fixture1.json
django/trunk/tests/modeltests/fixtures/models.py
Log:
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.
Modified: django/trunk/django/core/management/commands/dumpdata.py
===================================================================
--- django/trunk/django/core/management/commands/dumpdata.py 2009-02-28
04:52:16 UTC (rev 9920)
+++ django/trunk/django/core/management/commands/dumpdata.py 2009-02-28
05:35:22 UTC (rev 9921)
@@ -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 @@
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,10 +27,35 @@
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.
if format not in serializers.get_public_serializer_formats():
@@ -41,9 +67,13 @@
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:
Modified: django/trunk/docs/ref/django-admin.txt
===================================================================
--- django/trunk/docs/ref/django-admin.txt 2009-02-28 04:52:16 UTC (rev
9920)
+++ django/trunk/docs/ref/django-admin.txt 2009-02-28 05:35:22 UTC (rev
9921)
@@ -186,7 +186,7 @@
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 @@
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
-----
Modified: django/trunk/tests/modeltests/fixtures/fixtures/fixture1.json
===================================================================
--- django/trunk/tests/modeltests/fixtures/fixtures/fixture1.json
2009-02-28 04:52:16 UTC (rev 9920)
+++ django/trunk/tests/modeltests/fixtures/fixtures/fixture1.json
2009-02-28 05:35:22 UTC (rev 9921)
@@ -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
Modified: django/trunk/tests/modeltests/fixtures/models.py
===================================================================
--- django/trunk/tests/modeltests/fixtures/models.py 2009-02-28 04:52:16 UTC
(rev 9920)
+++ django/trunk/tests/modeltests/fixtures/models.py 2009-02-28 05:35:22 UTC
(rev 9921)
@@ -11,6 +11,16 @@
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 @@
>>> 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 @@
# 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 @@
# 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---