Ticket #5610: dumpdata-additions-5610.diff

File dumpdata-additions-5610.diff, 5.4 KB (added by David Reynolds, 16 years ago)

Fixed dumpdata as per discussion and added in tests

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

    diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py
    index e55ae7b..7bdcc42 100644
    a b class Command(BaseCommand):  
    1313    args = '[appname ...]'
    1414
    1515    def handle(self, *app_labels, **options):
    16         from django.db.models import get_app, get_apps, get_models, get_model
     16        from django.db.models import get_app, get_apps, get_models
    1717        from django.core import serializers
    1818
    1919        format = options.get('format', 'json')
    2020        indent = options.get('indent', None)
    21        
     21
    2222        if len(app_labels) == 0:
    2323            app_list = get_apps()
    2424        else:
    25             app_list = {}
    26             for app_label in app_labels:
    27                 app_model_label = app_label.split('.')
    28                 if len(app_model_label) <= 1:
    29                     # This is just an app
    30                     app_list[app_model_label[0]] = None
    31                 else:
    32                     # This is app.Model
    33                     if app_model_label[0] in app_list.keys() and app_list[app_model_label[0]]:
    34                             app_list[app_model_label[0]].append(app_model_label[1])
    35                     else:
    36                         app_list[app_model_label[0]] = [app_model_label[1]]
     25            app_list = [get_app(app_label) for app_label in app_labels]
    3726
    3827        # Check that the serialization format exists; this is a shortcut to
    3928        # avoid collating all the objects and _then_ failing.
    40        
    4129        try:
    4230            serializers.get_serializer(format)
    4331        except KeyError:
    4432            raise CommandError("Unknown serialization format: %s" % format)
    4533
    4634        objects = []
    47         for app_label in app_list:
    48             if app_list[app_label]:
    49                 for model_label in app_list[app_label]:
    50                     model = get_model(app_label, model_label)
    51                     objects.extend(model.objects.all())
    52             else:
    53                 app = get_app(app_label)
    54                 for model in get_models(app):
    55                     objects.extend(model.objects.all())
    56 
     35        for app in app_list:
     36            for model in get_models(app):
     37                objects.extend(model.objects.all())
    5738        try:
    5839            return serializers.serialize(format, objects, indent=indent)
    5940        except Exception, e:
  • tests/modeltests/fixtures/models.py

    diff --git a/tests/modeltests/fixtures/models.py b/tests/modeltests/fixtures/models.py
    index 51dad44..5b53115 100644
    a b class Article(models.Model):  
    2020
    2121    class Meta:
    2222        ordering = ('-pub_date', 'headline')
    23        
    24 class Category(models.Model):
    25     title = models.CharField(max_length=100)
    26     description = models.TextField()
    27    
    28     def __unicode__(self):
    29         return self.title
    30 
    31     class Meta:
    32         ordering = ('title',)
    3323
    3424__test__ = {'API_TESTS': """
    3525>>> from django.core import management
    Multiple fixtures named 'fixture2' in '...fixtures'. Aborting.  
    9282# Dump the current contents of the database as a JSON fixture
    9383>>> management.call_command('dumpdata', 'fixtures', format='json')
    9484[{"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"}}]
    11585"""
    11686
    11787from django.test import TestCase
Back to Top