Ticket #5610: dumpdata-addition-r9646.diff
File dumpdata-addition-r9646.diff, 6.1 KB (added by , 16 years ago) |
---|
-
django/core/management/commands/dumpdata.py
16 16 args = '[appname ...]' 17 17 18 18 def handle(self, *app_labels, **options): 19 from django.db.models import get_app, get_apps, get_models 20 19 from django.db.models import get_app, get_apps, get_models, get_model 21 20 format = options.get('format','json') 22 21 indent = options.get('indent',None) 23 22 exclude = options.get('exclude',[]) … … 28 27 if len(app_labels) == 0: 29 28 app_list = [app for app in get_apps() if app not in excluded_apps] 30 29 else: 31 app_list = [get_app(app_label) for app_label in app_labels] 30 app_list = {} 31 for app_label in app_labels: 32 app_model_label = app_label.split('.') 33 if len(app_model_label) <= 1: 34 # This is just an app 35 app_list[app_model_label[0]] = None 36 else: 37 # This is app.Model 38 if app_model_label[0] in app_list.keys() and app_list[app_model_label[0]]: 39 app_list[app_model_label[0]].append(app_model_label[1]) 40 else: 41 app_list[app_model_label[0]] = [app_model_label[1]] 32 42 33 43 # Check that the serialization format exists; this is a shortcut to 34 44 # avoid collating all the objects and _then_ failing. … … 42 52 43 53 objects = [] 44 54 for app in app_list: 45 for model in get_models(app): 46 objects.extend(model._default_manager.all()) 55 for app_label in app_list: 56 if app_list[app_label]: 57 for model_label in app_list[app_label]: 58 model = get_model(app_label, model_label) 59 objects.extend(model.objects.all()) 60 else: 61 app = get_app(app_label) 62 for model in get_models(app): 63 objects.extend(model.objects.all()) 47 64 try: 48 65 return serializers.serialize(format, objects, indent=indent) 49 66 except Exception, e: -
tests/modeltests/fixtures/models.py
11 11 from django.db import models 12 12 from django.conf import settings 13 13 14 class Category(models.Model): 15 title = models.CharField(max_length=100) 16 description = models.TextField() 17 18 def __unicode__(self): 19 return self.title 20 21 class Meta: 22 ordering = ('title',) 23 14 24 class Article(models.Model): 15 25 headline = models.CharField(max_length=100, default='Default headline') 16 26 pub_date = models.DateTimeField() … … 82 92 # Dump the current contents of the database as a JSON fixture 83 93 >>> management.call_command('dumpdata', 'fixtures', format='json') 84 94 [{"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"}}] 95 96 # Reset Database representation again. 97 >>> management.call_command('flush', verbosity=0, interactive=False) 98 >>> management.call_command('loaddata', 'fixture4', verbosity=0) 99 >>> Article.objects.all() 100 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] 101 >>> Category.objects.all() 102 [<Category: News Stories>] 103 104 # Dump the contents of the database as a JSON fixture 105 >>> management.call_command('dumpdata', 'fixtures', format='json') 106 [{"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"}}] 107 108 # Try just dumping the contents of fixtures.Category 109 >>> management.call_command('dumpdata', 'fixtures.Category', format='json') 110 [{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}] 111 112 # ..and just fixtures.Article 113 >>> management.call_command('dumpdata', 'fixtures.Article', format='json') 114 [{"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"}}] 85 115 """ 86 116 87 117 from django.test import TestCase -
docs/ref/django-admin.txt
194 194 dumpdata 195 195 -------- 196 196 197 .. django-admin:: dumpdata <appname appname ...>197 .. django-admin:: dumpdata <appname appname appname.Model ...> 198 198 199 199 Outputs to standard output all data in the database associated with the named 200 200 application(s). 201 201 202 202 If no application name is provided, all installed applications will be dumped. 203 203 204 Alternatively, you can provide a list of models in the form of ``appname.Model`` if you want to limit what models you dump. You can also provide a mixture of both. 205 204 206 The output of ``dumpdata`` can be used as input for ``loaddata``. 205 207 206 208 Note that ``dumpdata`` uses the default manager on the model for selecting the