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:43:38 -0500 |
| commit | d31424fec1a3de9d281535c0503644a9d7b93c63 (patch) | |
| tree | 934ab8ba163f645c2addc72098575253be1c4fb9 | |
| parent | 4d3b8e199ec00a0aba111637a25f5abcaf3d6dfc (diff) | |
[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
| -rw-r--r-- | django/contrib/contenttypes/fields.py | 15 | ||||
| -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, 5 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index 2227707203..42e06aa119 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 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 9add025a46..cfac484053 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 |
