Ticket #28099: delete_on_update_fix.diff
File delete_on_update_fix.diff, 4.6 KB (added by , 8 years ago) |
---|
-
django/db/models/deletion.py
old new 286 286 287 287 # update fields 288 288 for model, instances_for_fieldvalues in six.iteritems(self.field_updates): 289 query = sql.UpdateQuery(model)290 289 for (field, value), instances in six.iteritems(instances_for_fieldvalues): 290 query = sql.UpdateQuery(model) 291 291 query.update_batch([obj.pk for obj in instances], 292 292 {field.name: value}, self.using) 293 293 -
tests/delete_regress/models.py
diff --git a/tests/delete_regress/models.py b/tests/delete_regress/models.py index f0145de..1e6da6b 100644
a b class OrderedPerson(models.Model): 139 139 140 140 class Meta: 141 141 ordering = ['name'] 142 143 def organization_on_delete(collector, field, sub_objs, using): 144 deleted_objs = sub_objs.filter(report__isnull=True) 145 ignored_objs = sub_objs.filter(report__isnull=False) 146 collector.add_field_update(field, None, ignored_objs) 147 models.CASCADE(collector, field, deleted_objs, using) 148 149 150 class Organization(models.Model): 151 name = models.CharField(max_length=255) 152 153 class Report(models.Model): 154 name = models.CharField(max_length=255) 155 156 class ThreatenedOrganization(models.Model): 157 organization = models.ForeignKey(Organization, null=True, blank=True, 158 on_delete=organization_on_delete) 159 organization_name = models.CharField(max_length=255) 160 report = models.ForeignKey(Report, null=True, blank=True) 161 162 def __unicode__(self): 163 if self.report_id: 164 return 'reported threatened org' 165 return 'threatened org' 166 167 class CostClaim(models.Model): 168 organization = models.ForeignKey(Organization, null=True, blank=True, 169 on_delete=organization_on_delete) 170 organization_name = models.CharField(max_length=255) 171 report = models.ForeignKey(Report, null=True, blank=True) 172 threatened = models.ForeignKey(ThreatenedOrganization, null=True, blank=True, 173 on_delete=models.SET_NULL) 174 175 def __unicode__(self): 176 if self.report_id: 177 return 'reported cost claim' 178 return 'cost claim' 179 -
tests/delete_regress/tests.py
diff --git a/tests/delete_regress/tests.py b/tests/delete_regress/tests.py index 7472731..01257ba 100644
a b from .models import ( 7 7 Award, AwardNote, Book, Child, Eaten, Email, File, Food, FooFile, 8 8 FooFileProxy, FooImage, FooPhoto, House, Image, Item, Location, Login, 9 9 OrderedPerson, OrgUnit, Person, Photo, PlayedWith, PlayedWithNote, Policy, 10 Researcher, Toy, Version, 10 Researcher, Toy, Version, Organization, Report, ThreatenedOrganization, 11 CostClaim 11 12 ) 12 13 13 14 … … class OrderedDeleteTests(TestCase): 343 344 OrderedPerson.objects.create(name='Bob', lives_in=h) 344 345 OrderedPerson.objects.filter(lives_in__address='Foo').delete() 345 346 self.assertEqual(OrderedPerson.objects.count(), 0) 347 348 class CollectorUpdateTests(TestCase): 349 def test_delete_with_custom_handlers(self): 350 351 org, _ = Organization.objects.get_or_create(name="test org") 352 353 t_org, _ = ThreatenedOrganization.objects.get_or_create( 354 organization=org, organization_name="test org", 355 report=None) 356 357 cost_claim, _ = CostClaim.objects.get_or_create( 358 organization=org, organization_name="test org", 359 threatened=t_org, report=None) 360 361 report, _ = Report.objects.get_or_create(name="test report") 362 363 t_org_in_report, _ = ThreatenedOrganization.objects.get_or_create( 364 organization=org, organization_name="test org", 365 report=report) 366 367 cost_claim_in_report, _ = CostClaim.objects.get_or_create( 368 organization=org, organization_name="test org", 369 threatened=t_org_in_report, report=report) 370 371 org.delete() 372 373 try: 374 cost_claim = CostClaim.objects.get(id = cost_claim.pk) 375 self.assertTrue(False) 376 except CostClaim.DoesNotExist: 377 self.assertTrue(True) 378 379 cost_claim_in_report = CostClaim.objects.get( 380 id = cost_claim_in_report.pk) 381 self.assertEqual(cost_claim_in_report.threatened_id, t_org_in_report.pk) 382 self.assertEqual(cost_claim_in_report.report_id, report.pk) 383 self.assertEqual(cost_claim_in_report.organization_id, None)