Ticket #5610: dumpdata-addition-r9646.diff

File dumpdata-addition-r9646.diff, 6.1 KB (added by David Reynolds, 16 years ago)

Updated to work against latest trunk

  • django/core/management/commands/dumpdata.py

     
    1616    args = '[appname ...]'
    1717
    1818    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
    2120        format = options.get('format','json')
    2221        indent = options.get('indent',None)
    2322        exclude = options.get('exclude',[])
     
    2827        if len(app_labels) == 0:
    2928            app_list = [app for app in get_apps() if app not in excluded_apps]
    3029        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]]
    3242
    3343        # Check that the serialization format exists; this is a shortcut to
    3444        # avoid collating all the objects and _then_ failing.
     
    4252
    4353        objects = []
    4454        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())
    4764        try:
    4865            return serializers.serialize(format, objects, indent=indent)
    4966        except Exception, e:
  • tests/modeltests/fixtures/models.py

     
    1111from django.db import models
    1212from django.conf import settings
    1313
     14class 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
    1424class Article(models.Model):
    1525    headline = models.CharField(max_length=100, default='Default headline')
    1626    pub_date = models.DateTimeField()
     
    8292# Dump the current contents of the database as a JSON fixture
    8393>>> management.call_command('dumpdata', 'fixtures', format='json')
    8494[{"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"}}]
    85115"""
    86116
    87117from django.test import TestCase
  • docs/ref/django-admin.txt

     
    194194dumpdata
    195195--------
    196196
    197 .. django-admin:: dumpdata <appname appname ...>
     197.. django-admin:: dumpdata <appname appname appname.Model ...>
    198198
    199199Outputs to standard output all data in the database associated with the named
    200200application(s).
    201201
    202202If no application name is provided, all installed applications will be dumped.
    203203
     204Alternatively, 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
    204206The output of ``dumpdata`` can be used as input for ``loaddata``.
    205207
    206208Note that ``dumpdata`` uses the default manager on the model for selecting the
Back to Top