Ticket #7159: loaddata-delete-2.diff
File loaddata-delete-2.diff, 5.3 KB (added by , 16 years ago) |
---|
-
django/core/management/commands/loaddata.py
14 14 make_option('--verbosity', action='store', dest='verbosity', default='1', 15 15 type='choice', choices=['0', '1', '2'], 16 16 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), 17 make_option('--delete', action='store_true', dest='delete', default=False, 18 help='Deletes any items in the database which are not in the fixture'), 17 19 ) 18 20 help = 'Installs the named fixture(s) in the database.' 19 21 args = "fixture [fixture ...]" 22 23 def remove_objects_not_in(self, objects_to_keep, verbosity): 24 """ 25 Deletes all the objects in the database that are not in objects_to_keep. 26 - objects_to_keep: A map where the keys are classes, and the values are a 27 set of the objects of that class we should keep. 28 """ 29 for class_ in objects_to_keep.keys(): 20 30 31 current = class_.objects.all() 32 current_ids = set( [x.id for x in current] ) 33 keep_ids = set( [x.id for x in objects_to_keep[class_]] ) 34 35 remove_these_ones = current_ids.difference(keep_ids) 36 if remove_these_ones: 37 38 for obj in current: 39 if obj.id in remove_these_ones: 40 obj.delete() 41 if verbosity >= 2: 42 print "Deleted object: "+ unicode(obj) 43 44 if verbosity > 0 and remove_these_ones: 45 num_deleted = len(remove_these_ones) 46 if num_deleted > 1: 47 type_deleted = unicode(class_._meta.verbose_name_plural) 48 else: 49 type_deleted = unicode(class_._meta.verbose_name) 50 51 print "Deleted "+ str(num_deleted) +" "+ type_deleted 52 21 53 def handle(self, *fixture_labels, **options): 22 54 from django.db.models import get_apps 23 55 from django.core import serializers … … 28 60 29 61 verbosity = int(options.get('verbosity', 1)) 30 62 show_traceback = options.get('traceback', False) 31 63 is_delete = options.get('delete', False) 64 32 65 # Keep a count of the installed objects and fixtures 33 66 fixture_count = 0 34 67 object_count = 0 … … 104 137 print "Installing %s fixture '%s' from %s." % \ 105 138 (format, fixture_name, humanize(fixture_dir)) 106 139 try: 140 objects_to_keep = {} 107 141 objects = serializers.deserialize(format, fixture) 108 142 for obj in objects: 109 143 object_count += 1 110 144 objects_per_fixture[-1] += 1 111 models.add(obj.object.__class__) 145 146 class_ = obj.object.__class__ 147 if not class_ in objects_to_keep: 148 objects_to_keep[class_] = set() 149 objects_to_keep[class_].add(obj.object) 150 151 models.add(class_) 112 152 obj.save() 153 154 if is_delete: 155 self.remove_objects_not_in(objects_to_keep, verbosity) 156 113 157 label_found = True 114 158 except (SystemExit, KeyboardInterrupt): 115 159 raise -
tests/modeltests/fixtures/models.py
54 54 # object list is unaffected 55 55 >>> Article.objects.all() 56 56 [<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>] 57 58 # Delete two existing objects 59 >>> management.call_command('loaddata', 'fixture4.json', verbosity=0, delete=True) 60 >>> Article.objects.all() 61 [<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>] 57 62 """} 58 63 59 64 # Database flushing does not work on MySQL with the default storage engine -
docs/django-admin.txt
387 387 Example usage:: 388 388 389 389 django-admin.py loaddata --verbosity=2 390 391 **New in Django development version** 392 --delete 393 ~~~~~~~~ 390 394 395 Deletes any objects in the database which are not in the fixtures. If you run ``loaddata`` 396 with ``--delete``, after the operation the database will exactly match the contents 397 of the fixtures. 398 If you do not specify ``--delete`` new objects only present in the fixture will be created in 399 the database, objects present in both will be updated, but no data will be removed from 400 the database. 401 391 402 makemessages 392 403 ------------ 393 404