From 31a56e30cfb3c8e1a9373e5abdd12b5b23d5d910 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 10 Sep 2016 20:30:14 -0400 Subject: [PATCH] Fixed #27407 -- Made Model.delete(keep_parents=True) preserve parent reverse relationships. Thanks Tim for the review. --- django/db/models/deletion.py | 4 ++++ docs/releases/1.10.4.txt | 3 +++ tests/delete/tests.py | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/django/db/models/deletion.py b/django/db/models/deletion.py index 3830052ca3..26073be3ba 100644 --- a/django/db/models/deletion.py +++ b/django/db/models/deletion.py @@ -206,7 +206,11 @@ class Collector(object): collect_related=False, reverse_dependency=True) if collect_related: + parents = model._meta.parents for related in get_candidate_relations_to_delete(model._meta): + # Preserve parent reverse relationships if keep_parents=True. + if keep_parents and related.model in parents: + continue field = related.field if field.remote_field.on_delete == DO_NOTHING: continue diff --git a/docs/releases/1.10.4.txt b/docs/releases/1.10.4.txt index 3304df9071..4d3ad91cf4 100644 --- a/docs/releases/1.10.4.txt +++ b/docs/releases/1.10.4.txt @@ -16,3 +16,6 @@ Bugfixes * Fixed incorrect ``app_label`` / ``model_name`` arguments for ``allow_migrate()`` in ``makemigrations`` migration consistency checks (:ticket:`27461`). + +* Made ``Model.delete(keep_parents=True)`` preserve parent reverse + relationships in multi-table inheritance (:ticket:`27407`). diff --git a/tests/delete/tests.py b/tests/delete/tests.py index bb6dd9c0b6..f8203b2780 100644 --- a/tests/delete/tests.py +++ b/tests/delete/tests.py @@ -359,6 +359,15 @@ class DeletionTests(TestCase): self.assertFalse(RChild.objects.filter(id=child.id).exists()) self.assertTrue(R.objects.filter(id=parent_id).exists()) + def test_delete_with_keeping_parents_relationships(self): + child = RChild.objects.create() + parent_id = child.r_ptr_id + parent_referent_id = S.objects.create(r=child.r_ptr).pk + child.delete(keep_parents=True) + self.assertFalse(RChild.objects.filter(id=child.id).exists()) + self.assertTrue(R.objects.filter(id=parent_id).exists()) + self.assertTrue(S.objects.filter(pk=parent_referent_id).exists()) + def test_queryset_delete_returns_num_rows(self): """ QuerySet.delete() should return the number of deleted rows and a