From d31424fec1a3de9d281535c0503644a9d7b93c63 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 29 Nov 2017 01:06:45 -0500 Subject: [PATCH] [2.0.x] Fixed #28856 -- Fixed a regression in caching of a GenericForeignKey pointing to a MTI model. Regression in b9f8635f58ad743995cad2081b3dc395e55761e5. Modified backport of e50add6ca1605dcc06c8c5a5770342779a4d5124 from master --- django/contrib/contenttypes/fields.py | 15 ++++++++++----- docs/releases/1.11.8.txt | 3 +++ tests/generic_relations_regress/tests.py | 6 ++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index 22277072031..42e06aa119e 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -228,12 +228,17 @@ class GenericForeignKey(FieldCacheMixin): rel_obj = self.get_cached_value(instance, default=None) if rel_obj is not None: - ct_match = ct_id == self.get_content_type(obj=rel_obj, using=instance._state.db).id - pk_match = rel_obj._meta.pk.to_python(pk_val) == rel_obj.pk - if ct_match and pk_match: - return rel_obj - else: + if ct_id != self.get_content_type(obj=rel_obj, using=instance._state.db).id: rel_obj = None + else: + pk = rel_obj._meta.pk + # If the primary key is a remote field, use the referenced + # field's to_python(). + pk_to_python = pk.target_field.to_python if pk.remote_field else pk.to_python + if pk_to_python(pk_val) != rel_obj._get_pk_val(): + rel_obj = None + else: + return rel_obj if ct_id is not None: ct = self.get_content_type(id=ct_id, using=instance._state.db) try: diff --git a/docs/releases/1.11.8.txt b/docs/releases/1.11.8.txt index 959731bbd5c..596ea434ece 100644 --- a/docs/releases/1.11.8.txt +++ b/docs/releases/1.11.8.txt @@ -27,3 +27,6 @@ Bugfixes * Made query lookups for ``CICharField``, ``CIEmailField``, and ``CITextField`` use a ``citext`` cast (:ticket:`28702`). + +* Fixed a regression in caching of a ``GenericForeignKey`` when the referenced + model instance uses multi-table inheritance (:ticket:`28856`). diff --git a/tests/generic_relations_regress/tests.py b/tests/generic_relations_regress/tests.py index 9add025a46a..cfac484053a 100644 --- a/tests/generic_relations_regress/tests.py +++ b/tests/generic_relations_regress/tests.py @@ -48,6 +48,12 @@ class GenericRelationTests(TestCase): TextLink.objects.create(content_object=oddrel) oddrel.delete() + def test_coerce_object_id_remote_field_cache_persistence(self): + restaurant = Restaurant.objects.create() + CharLink.objects.create(content_object=restaurant) + charlink = CharLink.objects.latest('pk') + self.assertIs(charlink.content_object, charlink.content_object) + def test_q_object_or(self): """ SQL query parameters for generic relations are properly