diff options
| author | Simon Charette <charette.s@gmail.com> | 2017-11-29 01:06:45 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-11-30 10:45:20 -0500 |
| commit | f319e7abad145ddcb1017293b5cdb7e09a92ee85 (patch) | |
| tree | 972e6d097f41f4ffa0f2e8491c449c28d593b38c | |
| parent | 3545e844885608932a692d952c12cd863e2320b5 (diff) | |
[1.11.x] Fixed #28856 -- Fixed a regression in caching of a GenericForeignKey pointing to a MTI model.
Regression in b9f8635f58ad743995cad2081b3dc395e55761e5.
Backport of d31424fec1a3de9d281535c0503644a9d7b93c63 from stable/2.0.x
| -rw-r--r-- | django/contrib/contenttypes/fields.py | 13 | ||||
| -rw-r--r-- | docs/releases/1.11.8.txt | 3 | ||||
| -rw-r--r-- | tests/generic_relations_regress/tests.py | 6 |
3 files changed, 19 insertions, 3 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index 6c4eb43ac1..84aac50814 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -230,9 +230,16 @@ class GenericForeignKey(object): except AttributeError: rel_obj = None else: - if rel_obj and (ct_id != self.get_content_type(obj=rel_obj, using=instance._state.db).id or - rel_obj._meta.pk.to_python(pk_val) != rel_obj._get_pk_val()): - rel_obj = None + if rel_obj: + 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 if rel_obj is not None: return rel_obj diff --git a/docs/releases/1.11.8.txt b/docs/releases/1.11.8.txt index 959731bbd5..596ea434ec 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 b2d5b08692..e6d350aa5b 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 |
