Ticket #14162: dumpdata_change.diff

File dumpdata_change.diff, 5.9 KB (added by Paul McMillan, 14 years ago)

Patches dumpdata to use _base_manager if --all is set. Includes tests and docs. Fixes a stray comment.

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

     
    1919            help='An appname or appname.ModelName to exclude (use multiple --exclude to exclude multiple apps/models).'),
    2020        make_option('-n', '--natural', action='store_true', dest='use_natural_keys', default=False,
    2121            help='Use natural keys if they are available.'),
     22        make_option('-a', '--all', action='store_true', dest='use_base_manager', default=False,
     23            help="Use Django's base manager to dump all models stored in the database, including those that would otherwise be filtered or modified by a custom manager."),
    2224    )
    2325    help = ("Output the contents of the database as a fixture of the given "
    24             "format (using each model's default manager).")
     26            "format (using each model's default manager unless --all is "
     27            "specified).")
    2528    args = '[appname appname.ModelName ...]'
    2629
    2730    def handle(self, *app_labels, **options):
     
    3437        excludes = options.get('exclude',[])
    3538        show_traceback = options.get('traceback', False)
    3639        use_natural_keys = options.get('use_natural_keys', False)
     40        use_base_manager = options.get('use_base_manager', False)
    3741
    3842        excluded_apps = set()
    3943        excluded_models = set()
     
    100104            if model in excluded_models:
    101105                continue
    102106            if not model._meta.proxy and router.allow_syncdb(using, model):
    103                 objects.extend(model._default_manager.using(using).all())
     107                if use_base_manager:
     108                    objects.extend(model._base_manager.using(using).all())
     109                else:
     110                    objects.extend(model._default_manager.using(using).all())
    104111
    105112        try:
    106113            return serializers.serialize(format, objects, indent=indent,
  • tests/modeltests/fixtures/tests.py

     
    66from django.core import management
    77from django.db import DEFAULT_DB_ALIAS
    88
    9 from models import Article, Blog, Book, Category, Person, Tag, Visa
     9from models import Article, Blog, Book, Category, Person, Spy, Tag, Visa
    1010
    1111class TestCaseFixtureLoadingTests(TestCase):
    1212    fixtures = ['fixture1.json', 'fixture2.json']
     
    2424class FixtureLoadingTests(TestCase):
    2525
    2626    def _dumpdata_assert(self, args, output, format='json', natural_keys=False,
    27                          exclude_list=[]):
     27                         use_base_manager=False, exclude_list=[]):
    2828        new_io = StringIO.StringIO()
    2929        management.call_command('dumpdata', *args, **{'format':format,
    3030                                                      'stdout':new_io,
    3131                                                      'stderr':new_io,
    3232                                                      'use_natural_keys':natural_keys,
     33                                                      'use_base_manager':use_base_manager,
    3334                                                      'exclude': exclude_list})
    3435        command_output = new_io.getvalue().strip()
    3536        self.assertEqual(command_output, output)
     
    197198                          '',
    198199                          exclude_list=['fixtures.FooModel'])
    199200
     201    def test_dumpdata_with_filtering_manager(self):
     202        Spy(name='Paul').save()
     203        Spy(name='Alex', cover_blown=True).save()
     204        self.assertQuerysetEqual(Spy.objects.all(),
     205                                 ['<Spy: Paul>'])
     206        #use the default manager
     207        self._dumpdata_assert(['fixtures.Spy'],'[{"pk": 1, "model": "fixtures.spy", "fields": {"cover_blown": false}}]')
     208        #dump using Django's base manager. Should return all objects,
     209        #even those normally filtered by the manager
     210        self._dumpdata_assert(['fixtures.Spy'], '[{"pk": 2, "model": "fixtures.spy", "fields": {"cover_blown": true}}, {"pk": 1, "model": "fixtures.spy", "fields": {"cover_blown": false}}]', use_base_manager=True)
     211       
     212
    200213    def test_compress_format_loading(self):
    201214        # Load fixture 4 (compressed), using format specification
    202215        management.call_command('loaddata', 'fixture4.json', verbosity=0, commit=False)
  • tests/modeltests/fixtures/models.py

     
    7272    def natural_key(self):
    7373        return (self.name,)
    7474
     75class SpyManager(PersonManager):
     76    def get_query_set(self):
     77        return super(SpyManager, self).get_query_set().filter(cover_blown=False)
     78
     79class Spy(Person):
     80    objects = SpyManager()
     81    cover_blown = models.BooleanField(default=False)
     82
    7583class Visa(models.Model):
    7684    person = models.ForeignKey(Person)
    7785    permissions = models.ManyToManyField(Permission, blank=True)
  • docs/ref/django-admin.txt

     
    208208the default manager and it filters some of the available records, not all of the
    209209objects will be dumped.
    210210
     211.. versionadded:: 1.3
     212
     213The :djadminopt:`--all` option may be provided to specify that
     214``dumpdata`` should use Django's base manager, dumping records which
     215might otherwise be filtered or modified by a custom manager.
     216
    211217.. django-admin-option:: --format <fmt>
    212218
    213219By default, ``dumpdata`` will format its output in JSON, but you can use the
     
    271277The :djadminopt:`--database` option may be used to specify the database
    272278to flush.
    273279
    274 
    275280inspectdb
    276281---------
    277282
Back to Top