From 584e10c76edcd3a33765a52c5cde0096a3ce7dbc Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 3 May 2017 10:39:54 -0400 Subject: [PATCH] Fixed #28166 -- Fixed Model._state.db on MTI parent model after saving child model. Regression in 38575b007a722d6af510ea46d46393a4cda9ca29. --- django/db/models/fields/related_descriptors.py | 4 +++- docs/releases/1.11.1.txt | 4 ++++ tests/model_inheritance_regress/tests.py | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/db/models/fields/related_descriptors.py b/django/db/models/fields/related_descriptors.py index 5899a2324c..4f3c08a4b0 100644 --- a/django/db/models/fields/related_descriptors.py +++ b/django/db/models/fields/related_descriptors.py @@ -271,7 +271,9 @@ class ForwardOneToOneDescriptor(ForwardManyToOneDescriptor): # on the related model for every deferred field. if not any(field in fields for field in deferred): kwargs = {field: getattr(instance, field) for field in fields} - return rel_model(**kwargs) + obj = rel_model(**kwargs) + obj._state.db = instance._state.db + return obj return super().get_object(instance) diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index 727ce94751..acce5259af 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -84,3 +84,7 @@ Bugfixes * Fixed crash in ``BaseInlineFormSet._construct_form()`` when using ``save_as_new`` (:ticket:`28159`). + +* Fixed a regression where ``Model._state.db`` wasn't set correctly on + multi-table inheritance parent models after saving a child model + (:ticket:`28166`). diff --git a/tests/model_inheritance_regress/tests.py b/tests/model_inheritance_regress/tests.py index 9cb710c4b8..87ec11a5b1 100644 --- a/tests/model_inheritance_regress/tests.py +++ b/tests/model_inheritance_regress/tests.py @@ -472,6 +472,10 @@ class ModelInheritanceTest(TestCase): jane = Supplier.objects.order_by("name").select_related("restaurant")[0] self.assertEqual(jane.restaurant.name, "Craft") + def test_ptr_accessor_assigns_db(self): + r = Restaurant.objects.create() + self.assertEqual(r.place_ptr._state.db, 'default') + def test_related_filtering_query_efficiency_ticket_15844(self): r = Restaurant.objects.create( name="Guido's House of Pasta",