Ticket #18100: ticket-18100-with-test.diff

File ticket-18100-with-test.diff, 3.1 KB (added by Simon Charette, 13 years ago)
  • django/db/models/deletion.py

    diff --git a/django/db/models/deletion.py b/django/db/models/deletion.py
    index d8bb8f2..22de2a9 100644
    a b def related_objects(self, related, objs):  
    200200
    201201    def instances_with_model(self):
    202202        for model, instances in self.data.iteritems():
     203            if model._deferred:
     204                # If the model has deferred fields we make sure to use the
     205                # underlying model class to dispatch correct signals #18100.
     206                model = model._meta.proxy_for_model
    203207            for obj in instances:
    204208                yield model, obj
    205209
  • tests/regressiontests/delete_regress/tests.py

    diff --git a/tests/regressiontests/delete_regress/tests.py b/tests/regressiontests/delete_regress/tests.py
    index 32feae2..4678ac8 100644
    a b  
    33import datetime
    44
    55from django.conf import settings
    6 from django.db import backend, transaction, DEFAULT_DB_ALIAS
     6from django.db import backend, models, transaction, DEFAULT_DB_ALIAS
    77from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
    88
    99from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith,
    def test_delete_proxy_pair(self):  
    258258        Image.objects.all().delete()
    259259
    260260        self.assertEqual(len(FooFileProxy.objects.all()), 0)
     261
     262
     263class DeletionSignalsTest(TestCase):
     264
     265    senders = (FooFile, FooFileProxy,)
     266
     267    def setUp(self):
     268        self.clear_received_senders()
     269        for sender in self.senders:
     270            models.signals.pre_delete.connect(self.pre_delete_receiver, sender)
     271            models.signals.post_delete.connect(self.post_delete_receiver, sender)
     272
     273    def tearDown(self):
     274        for sender in self.senders:
     275            models.signals.pre_delete.disconnect(self.pre_delete_receiver, sender)
     276            models.signals.post_delete.disconnect(self.post_delete_receiver, sender)
     277
     278    def clear_received_senders(self):
     279        self.pre_delete_senders = []
     280        self.post_delete_senders = []
     281
     282    def pre_delete_receiver(self, sender, **kwargs):
     283        self.pre_delete_senders.append(sender)
     284
     285    def post_delete_receiver(self, sender, **kwargs):
     286        self.post_delete_senders.append(sender)
     287
     288    def test_delete_deferred_model(self):
     289        """
     290        Model with deferred fields are actually proxy models under the hood.
     291        Make sure deletion signals are correctly sent when fields are deferred
     292        on a concrete model and on a proxy.
     293
     294        Refs #18100
     295        """
     296        my_file = File.objects.create()
     297
     298        obj = FooFile.objects.create(my_file=my_file)
     299        FooFile.objects.only('id').get(id=obj.id).delete()
     300        self.assertEqual(self.pre_delete_senders, [FooFile])
     301        self.assertEqual(self.post_delete_senders, [FooFile])
     302
     303        self.clear_received_senders()
     304
     305        obj = FooFileProxy.objects.create(my_file=my_file)
     306        FooFileProxy.objects.only('id').get(id=obj.id).delete()
     307        self.assertEqual(self.pre_delete_senders, [FooFileProxy])
     308        self.assertEqual(self.post_delete_senders, [FooFileProxy])
     309
Back to Top