summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2017-11-29 01:06:45 -0500
committerTim Graham <timograham@gmail.com>2017-11-30 10:45:20 -0500
commitf319e7abad145ddcb1017293b5cdb7e09a92ee85 (patch)
tree972e6d097f41f4ffa0f2e8491c449c28d593b38c /django
parent3545e844885608932a692d952c12cd863e2320b5 (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
Diffstat (limited to 'django')
-rw-r--r--django/contrib/contenttypes/fields.py13
1 files changed, 10 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