summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJamie Matthews <jamie@dabapps.com>2022-01-04 11:10:49 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-01-05 09:49:05 +0100
commitf5233dce309543c826224be9dfa9c9f4f855f73c (patch)
tree1c16a519509aa0829c884838815875efff3dcbe4 /django
parent973fa566521037ac140dcece73fceae50ee522f1 (diff)
Fixed #32511 -- Corrected handling prefetched nested reverse relationships.
When prefetching a set of child objects related to a set of parent objects, we usually want to populate the relationship back from the child to the parent to avoid a query when accessing that relationship attribute. However, there's an edge case where the child queryset itself specifies a prefetch back to the parent. In that case, we want to use the prefetched relationship rather than populating the reverse relationship from the parent.
Diffstat (limited to 'django')
-rw-r--r--django/db/models/fields/related_descriptors.py5
1 files changed, 3 insertions, 2 deletions
diff --git a/django/db/models/fields/related_descriptors.py b/django/db/models/fields/related_descriptors.py
index d5aa968400..9c50ef16ce 100644
--- a/django/db/models/fields/related_descriptors.py
+++ b/django/db/models/fields/related_descriptors.py
@@ -646,8 +646,9 @@ def create_reverse_many_to_one_manager(superclass, rel):
# Since we just bypassed this class' get_queryset(), we must manage
# the reverse relation manually.
for rel_obj in queryset:
- instance = instances_dict[rel_obj_attr(rel_obj)]
- setattr(rel_obj, self.field.name, instance)
+ if not self.field.is_cached(rel_obj):
+ instance = instances_dict[rel_obj_attr(rel_obj)]
+ setattr(rel_obj, self.field.name, instance)
cache_name = self.field.remote_field.get_cache_name()
return queryset, rel_obj_attr, instance_attr, False, cache_name, False