1
0
mirror of https://github.com/django/django.git synced 2025-06-14 16:09:12 +00:00

[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
This commit is contained in:
Simon Charette 2017-11-29 01:06:45 -05:00 committed by Tim Graham
parent 3545e84488
commit f319e7abad
3 changed files with 19 additions and 3 deletions

View File

@ -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

View File

@ -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`).

View File

@ -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