Ticket #7159: loaddata-delete.diff
File loaddata-delete.diff, 5.4 KB (added by , 17 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 ...]" 20 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(): 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 … … 97 130 print "Installing %s fixture '%s' from %s." % \ 98 131 (format, fixture_name, humanize(fixture_dir)) 99 132 try: 133 134 objects_to_keep = {} 135 100 136 objects = serializers.deserialize(format, fixture) 101 137 for obj in objects: 102 138 object_count += 1 103 models.add(obj.object.__class__) 139 140 class_ = obj.object.__class__ 141 if not class_ in objects_to_keep: 142 objects_to_keep[class_] = set() 143 objects_to_keep[class_].add(obj.object) 144 145 models.add(class_) 104 146 obj.save() 147 148 if is_delete: 149 self.remove_objects_not_in(objects_to_keep, verbosity) 150 105 151 label_found = True 106 152 except Exception, e: 107 153 fixture.close() -
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
321 321 322 322 django-admin.py loaddata --verbosity=2 323 323 324 **New in Django development version** 325 --delete 326 ~~~~~~~~ 327 328 Deletes any objects in the database which are not in the fixtures. If you run ``loaddata`` 329 with ``--delete``, after the operation the database will exactly match the contents 330 of the fixtures. 331 If you do not specify ``--delete`` new objects only present in the fixture will be created in 332 the database, objects present in both will be updated, but no data will be removed from 333 the database. 334 324 335 reset <appname appname ...> 325 336 --------------------------- 326 337