From eb7a329fb65e93dcd6c773c167353af9825d7b6e Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 20 Nov 2015 14:18:47 -0500 Subject: [PATCH] [1.9.x] Fixed #25685 -- Fixed a duplicate query regression on deletion of proxied models. Thanks to Trac alias ppetrid for the report and Tim for the review. Conflicts: tests/delete/tests.py Forward port of 7c3ef19978b36b61db88a519f799f1ce8d019679 from stable/1.8.x --- django/db/models/deletion.py | 4 ++-- docs/releases/1.8.7.txt | 3 +++ tests/delete/models.py | 6 ++++++ tests/delete/tests.py | 11 +++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/django/db/models/deletion.py b/django/db/models/deletion.py index a8c98f5b83f..e067559ccb1 100644 --- a/django/db/models/deletion.py +++ b/django/db/models/deletion.py @@ -58,9 +58,9 @@ def get_candidate_relations_to_delete(opts): candidate_models = {opts} candidate_models = candidate_models.union(opts.concrete_model._meta.proxied_children) # For each model, get all candidate fields. - candidate_model_fields = chain.from_iterable( + candidate_model_fields = set(chain.from_iterable( opts.get_fields(include_hidden=True) for opts in candidate_models - ) + )) # The candidate relations are the ones that come from N-1 and 1-1 relations. # N-N (i.e., many-to-many) relations aren't candidates for deletion. return ( diff --git a/docs/releases/1.8.7.txt b/docs/releases/1.8.7.txt index 8ffae59b216..df0ff411597 100644 --- a/docs/releases/1.8.7.txt +++ b/docs/releases/1.8.7.txt @@ -39,3 +39,6 @@ Bugfixes * Fixed ``Model.refresh_from_db()`` updating of ``ForeignKey`` fields with ``on_delete=models.SET_NULL`` (:ticket:`25715`). + +* Fixed a duplicate query regression in 1.8 on proxied model deletion + (:ticket:`25685`). diff --git a/tests/delete/models.py b/tests/delete/models.py index a7838c4510c..60e7f5957c1 100644 --- a/tests/delete/models.py +++ b/tests/delete/models.py @@ -99,6 +99,12 @@ class Avatar(models.Model): desc = models.TextField(null=True) +# This model is used to test a duplicate query regression (#25685) +class AvatarProxy(Avatar): + class Meta: + proxy = True + + class User(models.Model): avatar = models.ForeignKey(Avatar, models.CASCADE, null=True) diff --git a/tests/delete/tests.py b/tests/delete/tests.py index 60c61ed2811..c50f95e1262 100644 --- a/tests/delete/tests.py +++ b/tests/delete/tests.py @@ -413,6 +413,17 @@ class DeletionTests(TestCase): for k, v in existed_objs.items(): self.assertEqual(deleted_objs[k], v) + def test_proxied_model_duplicate_queries(self): + """ + #25685 - Deleting instances of a model with existing proxy + classes should not issue multiple queries during cascade + deletion of referring models. + """ + avatar = Avatar.objects.create() + # One query for the Avatar table and a second for the User one. + with self.assertNumQueries(2): + avatar.delete() + class FastDeleteTests(TestCase):