Ticket #5610: dumpdata-additions-trunk-5610.diff

File dumpdata-additions-trunk-5610.diff, 7.1 KB (added by David Reynolds, 10 years ago)

Should now be able to be applied to trunk.

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

     
    1414    args = '[appname ...]'
    1515
    1616    def handle(self, *app_labels, **options):
    17         from django.db.models import get_app, get_apps, get_models
     17        from django.db.models import get_app, get_apps, get_models, get_model
    1818
    1919        format = options.get('format', 'json')
    2020        indent = options.get('indent', None)
     
    2323        if len(app_labels) == 0:
    2424            app_list = get_apps()
    2525        else:
    26             app_list = [get_app(app_label) for app_label in app_labels]
     26            app_list = {}
     27            for app_label in app_labels:
     28                app_model_label = app_label.split('.')
     29                if len(app_model_label) <= 1:
     30                    # This is just an app
     31                    app_list[app_model_label[0]] = None
     32                else:
     33                    # This is app.Model
     34                    if app_model_label[0] in app_list.keys() and app_list[app_model_label[0]]:
     35                        app_list[app_model_label[0]].append(app_model_label[1])
     36                    else:
     37                        app_list[app_model_label[0]] = [app_model_label[1]]
    2738
    2839        # Check that the serialization format exists; this is a shortcut to
    2940        # avoid collating all the objects and _then_ failing.
     
    3647            raise CommandError("Unknown serialization format: %s" % format)
    3748
    3849        objects = []
    39         for app in app_list:
    40             for model in get_models(app):
    41                 objects.extend(model._default_manager.all())
     50        for app_label in app_list:
     51            if app_list[app_label]:
     52                for model_label in app_list[app_label]:
     53                    model = get_model(app_label, model_label)
     54                    objects.extend(model.objects.all())
     55            else:
     56                app = get_app(app_label)
     57                for model in get_models(app):
     58                    objects.extend(model.objects.all())
    4259        try:
    4360            return serializers.serialize(format, objects, indent=indent)
    4461        except Exception, e:
  • tests/modeltests/fixtures/fixtures/fixture4.json

     
     1[
     2    {
     3        "pk": "2",
     4        "model": "fixtures.article",
     5        "fields": {
     6            "headline": "Poker has no place on ESPN",
     7            "pub_date": "2006-06-16 12:00:00"
     8        }
     9    },
     10    {
     11        "pk": "3",
     12        "model": "fixtures.article",
     13        "fields": {
     14            "headline": "Time to reform copyright",
     15            "pub_date": "2006-06-16 13:00:00"
     16        }
     17    },
     18    {
     19        "pk" : "1",
     20        "model": "fixtures.category",
     21        "fields": {
     22            "title" : "News Stories",
     23            "description" : "Latest news stories"
     24        }
     25    }
     26]
     27 No newline at end of file
  • tests/modeltests/fixtures/models.py

     
    2121    class Meta:
    2222        ordering = ('-pub_date', 'headline')
    2323
     24class 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',)
     33
    2434__test__ = {'API_TESTS': """
    2535>>> from django.core import management
    2636>>> from django.db.models import get_app
     
    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/django-admin.txt

     
    122122Note that Django's default settings live in ``django/conf/global_settings.py``,
    123123if you're ever curious to see the full list of defaults.
    124124
    125 dumpdata <appname appname ...>
     125dumpdata <appname appname appname.Model ...>
    126126------------------------------
    127127
    128128Outputs to standard output all data in the database associated with the named
     
    130130
    131131If no application name is provided, all installed applications will be dumped.
    132132
     133Alternatively, you can provide a list of models in the form of ``appname.Model``
     134if  you want to limit what models you dump. You can also provide a mixture of
     135both.                                                                         
     136
    133137The output of ``dumpdata`` can be used as input for ``loaddata``.
    134138
    135139Note that ``dumpdata`` uses the default manager on the model for selecting the
Back to Top