summaryrefslogtreecommitdiff
path: root/django/contrib/contenttypes
diff options
context:
space:
mode:
authorClifford Gama <cliffygamy@gmail.com>2025-03-15 11:13:46 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-03-26 09:55:38 +0100
commitd5c19f9b327fccc1f707e10de648fe9d4cdacb9b (patch)
tree5d9d0aa6b358b1b0204c2917192c1039081b56ac /django/contrib/contenttypes
parent7d9aab8da0a06787d649762a702e1a518d843a63 (diff)
Fixed #34819 -- Made GenericForeignKey prefetching use matching pk representations.
Ensured that rel_obj_attr and instance_attr return matching (pk, cls) tuples in GenericForeignKey.get_prefetch_queryset(), preventing mismatches when prefetching related objects where pk and get_prep_value() differ. Using value_to_string() also makes this code compatible with composite primary keys.
Diffstat (limited to 'django/contrib/contenttypes')
-rw-r--r--django/contrib/contenttypes/fields.py7
1 files changed, 2 insertions, 5 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py
index 272cb2b02c..f9d896bc13 100644
--- a/django/contrib/contenttypes/fields.py
+++ b/django/contrib/contenttypes/fields.py
@@ -205,14 +205,11 @@ class GenericForeignKey(FieldCacheMixin, Field):
model = self.get_content_type(
id=ct_id, using=obj._state.db
).model_class()
- return (
- model._meta.pk.get_prep_value(getattr(obj, self.fk_field)),
- model,
- )
+ return str(getattr(obj, self.fk_field)), model
return (
ret_val,
- lambda obj: (obj.pk, obj.__class__),
+ lambda obj: (obj._meta.pk.value_to_string(obj), obj.__class__),
gfk_key,
True,
self.name,