Ticket #14162: dumpdata_change.diff
File dumpdata_change.diff, 5.9 KB (added by , 14 years ago) |
---|
-
django/core/management/commands/dumpdata.py
19 19 help='An appname or appname.ModelName to exclude (use multiple --exclude to exclude multiple apps/models).'), 20 20 make_option('-n', '--natural', action='store_true', dest='use_natural_keys', default=False, 21 21 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."), 22 24 ) 23 25 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).") 25 28 args = '[appname appname.ModelName ...]' 26 29 27 30 def handle(self, *app_labels, **options): … … 34 37 excludes = options.get('exclude',[]) 35 38 show_traceback = options.get('traceback', False) 36 39 use_natural_keys = options.get('use_natural_keys', False) 40 use_base_manager = options.get('use_base_manager', False) 37 41 38 42 excluded_apps = set() 39 43 excluded_models = set() … … 100 104 if model in excluded_models: 101 105 continue 102 106 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()) 104 111 105 112 try: 106 113 return serializers.serialize(format, objects, indent=indent, -
tests/modeltests/fixtures/tests.py
6 6 from django.core import management 7 7 from django.db import DEFAULT_DB_ALIAS 8 8 9 from models import Article, Blog, Book, Category, Person, Tag, Visa9 from models import Article, Blog, Book, Category, Person, Spy, Tag, Visa 10 10 11 11 class TestCaseFixtureLoadingTests(TestCase): 12 12 fixtures = ['fixture1.json', 'fixture2.json'] … … 24 24 class FixtureLoadingTests(TestCase): 25 25 26 26 def _dumpdata_assert(self, args, output, format='json', natural_keys=False, 27 exclude_list=[]):27 use_base_manager=False, exclude_list=[]): 28 28 new_io = StringIO.StringIO() 29 29 management.call_command('dumpdata', *args, **{'format':format, 30 30 'stdout':new_io, 31 31 'stderr':new_io, 32 32 'use_natural_keys':natural_keys, 33 'use_base_manager':use_base_manager, 33 34 'exclude': exclude_list}) 34 35 command_output = new_io.getvalue().strip() 35 36 self.assertEqual(command_output, output) … … 197 198 '', 198 199 exclude_list=['fixtures.FooModel']) 199 200 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 200 213 def test_compress_format_loading(self): 201 214 # Load fixture 4 (compressed), using format specification 202 215 management.call_command('loaddata', 'fixture4.json', verbosity=0, commit=False) -
tests/modeltests/fixtures/models.py
72 72 def natural_key(self): 73 73 return (self.name,) 74 74 75 class SpyManager(PersonManager): 76 def get_query_set(self): 77 return super(SpyManager, self).get_query_set().filter(cover_blown=False) 78 79 class Spy(Person): 80 objects = SpyManager() 81 cover_blown = models.BooleanField(default=False) 82 75 83 class Visa(models.Model): 76 84 person = models.ForeignKey(Person) 77 85 permissions = models.ManyToManyField(Permission, blank=True) -
docs/ref/django-admin.txt
208 208 the default manager and it filters some of the available records, not all of the 209 209 objects will be dumped. 210 210 211 .. versionadded:: 1.3 212 213 The :djadminopt:`--all` option may be provided to specify that 214 ``dumpdata`` should use Django's base manager, dumping records which 215 might otherwise be filtered or modified by a custom manager. 216 211 217 .. django-admin-option:: --format <fmt> 212 218 213 219 By default, ``dumpdata`` will format its output in JSON, but you can use the … … 271 277 The :djadminopt:`--database` option may be used to specify the database 272 278 to flush. 273 279 274 275 280 inspectdb 276 281 --------- 277 282