From 211486f3ab5602cdc332f03cd03fad3cb929d154 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Mon, 5 Oct 2015 13:26:14 -0400 Subject: [PATCH] Fixed #23076, #25505 -- Fixed deletion of intermediate proxy models. Thanks to James Murty for his work on an alternate patch. --- django/db/models/deletion.py | 2 +- tests/proxy_model_inheritance/models.py | 7 ++++++- tests/proxy_model_inheritance/tests.py | 8 ++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/django/db/models/deletion.py b/django/db/models/deletion.py index 831e3c03db4..76a2d35890c 100644 --- a/django/db/models/deletion.py +++ b/django/db/models/deletion.py @@ -55,7 +55,7 @@ def get_candidate_relations_to_delete(opts): # 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 ( - f for f in opts.get_fields(include_hidden=True) + f for f in opts.concrete_model._meta.get_fields(include_hidden=True) if f.auto_created and not f.concrete and (f.one_to_one or f.one_to_many) ) diff --git a/tests/proxy_model_inheritance/models.py b/tests/proxy_model_inheritance/models.py index 7a10b77c903..240fd60c4ec 100644 --- a/tests/proxy_model_inheritance/models.py +++ b/tests/proxy_model_inheritance/models.py @@ -6,7 +6,12 @@ class ConcreteModel(models.Model): pass -class ConcreteModelSubclass(ConcreteModel): +class ProxyModel(ConcreteModel): + class Meta: + proxy = True + + +class ConcreteModelSubclass(ProxyModel): pass diff --git a/tests/proxy_model_inheritance/tests.py b/tests/proxy_model_inheritance/tests.py index b0af0040f90..b4b1c134351 100644 --- a/tests/proxy_model_inheritance/tests.py +++ b/tests/proxy_model_inheritance/tests.py @@ -9,6 +9,7 @@ from django.utils._os import upath from .models import ( ConcreteModel, ConcreteModelSubclass, ConcreteModelSubclassProxy, + ProxyModel, ) @@ -43,3 +44,10 @@ class MultiTableInheritanceProxyTest(TestCase): self.assertEqual(0, ConcreteModelSubclassProxy.objects.count()) self.assertEqual(0, ConcreteModelSubclass.objects.count()) self.assertEqual(0, ConcreteModel.objects.count()) + + def test_deletion_through_intermediate_proxy(self): + child = ConcreteModelSubclass.objects.create() + proxy = ProxyModel.objects.get(pk=child.pk) + proxy.delete() + self.assertFalse(ConcreteModel.objects.exists()) + self.assertFalse(ConcreteModelSubclass.objects.exists())