Ticket #28099: delete_on_update_bug_update.diff

File delete_on_update_bug_update.diff, 3.1 KB (added by milosu, 8 years ago)
  • django/db/models/deletion.py

    diff --git a/django/db/models/deletion.py b/django/db/models/deletion.py
    index 5370114..753f4f5 100644
    a b class Collector:  
    285285
    286286            # update fields
    287287            for model, instances_for_fieldvalues in self.field_updates.items():
    288                 query = sql.UpdateQuery(model)
    289288                for (field, value), instances in instances_for_fieldvalues.items():
     289                    query = sql.UpdateQuery(model)
    290290                    query.update_batch([obj.pk for obj in instances],
    291291                                       {field.name: value}, self.using)
    292292
  • tests/delete_regress/models.py

    diff --git a/tests/delete_regress/models.py b/tests/delete_regress/models.py
    index f0145de..981be50 100644
    a b class OrderedPerson(models.Model):  
    139139
    140140    class Meta:
    141141        ordering = ['name']
     142
     143def status_on_delete(collector, field, sub_objs, using):
     144    priority1 = sub_objs.filter(priority=1)
     145    priority2 = sub_objs.filter(priority=2)
     146
     147    levelA_field = field.model._meta.get_field('levelA')
     148    levelB_field = field.model._meta.get_field('levelB')
     149
     150    collector.add_field_update(levelA_field, 11, priority1)
     151    collector.add_field_update(levelB_field, 12, priority2)
     152
     153    models.SET_NULL(collector, field, sub_objs, using)
     154   
     155
     156class Status(models.Model):
     157    pass
     158
     159
     160class Report(models.Model):
     161    status = models.ForeignKey(Status, null=True, blank=True,
     162                               on_delete=status_on_delete)
     163    priority = models.IntegerField()
     164    levelA = models.IntegerField()
     165    levelB = models.IntegerField()
  • tests/delete_regress/tests.py

    diff --git a/tests/delete_regress/tests.py b/tests/delete_regress/tests.py
    index 7472731..81d384a 100644
    a b from .models import (  
    77    Award, AwardNote, Book, Child, Eaten, Email, File, Food, FooFile,
    88    FooFileProxy, FooImage, FooPhoto, House, Image, Item, Location, Login,
    99    OrderedPerson, OrgUnit, Person, Photo, PlayedWith, PlayedWithNote, Policy,
    10     Researcher, Toy, Version,
    11 )
     10    Researcher, Toy, Version, Status, Report)
    1211
    1312
    1413# Can't run this test under SQLite, because you can't
    class OrderedDeleteTests(TestCase):  
    343342        OrderedPerson.objects.create(name='Bob', lives_in=h)
    344343        OrderedPerson.objects.filter(lives_in__address='Foo').delete()
    345344        self.assertEqual(OrderedPerson.objects.count(), 0)
     345
     346class CollectorUpdateTests(TestCase):
     347    def test_delete_with_custom_handlers(self):
     348
     349        status, _ = Status.objects.get_or_create()
     350
     351        p1 = Report.objects.create(status=status, priority=1, levelA=1, levelB=0)
     352
     353        p2 = Report.objects.create(status=status, priority=2, levelA=2, levelB=0)
     354
     355        status.delete()
     356
     357        p1 = Report.objects.get(id=p1.pk)
     358        self.assertEqual(p1.levelA, 11)
     359        self.assertEqual(p1.levelB, 0)
     360
     361        p2 = Report.objects.get(id=p2.pk)
     362        self.assertEqual(p2.levelA, 2)
     363        self.assertEqual(p2.levelB, 12)
Back to Top