diff options
| author | Martin Svoboda <martin.svoboda@gmail.com> | 2021-08-11 16:42:43 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-10-14 13:07:24 +0200 |
| commit | dd8945d361d0c0f3183f32dfc729f28726c005f8 (patch) | |
| tree | 21ece213e9fbf0c5158c3df10fcc0219ee7b8fbc /django | |
| parent | 8ab95364b5182fd702478289f98b01d16a21016e (diff) | |
[4.0.x] Fixed #33008 -- Fixed prefetch_related() for deleted GenericForeignKeys.
Thanks Simon Charette for the implementation idea.
Backport of cc4cb95beff0b75ec169add7e94cc481624a41e6 from main
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/contenttypes/fields.py | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index fbd55606ae..63ee408bf5 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -213,7 +213,7 @@ class GenericForeignKey(FieldCacheMixin): gfk_key, True, self.name, - True, + False, ) def __get__(self, instance, cls=None): @@ -229,6 +229,8 @@ class GenericForeignKey(FieldCacheMixin): pk_val = getattr(instance, self.fk_field) rel_obj = self.get_cached_value(instance, default=None) + if rel_obj is None and self.is_cached(instance): + return rel_obj 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 |
