1
0
mirror of https://github.com/django/django.git synced 2025-10-29 08:36:09 +00:00

Fixed #22650 -- Fixed regression on prefetch_related.

Regression from f51c1f59 when using select_related then prefetch_related
on the reverse side of an O2O:

Author.objects.select_related('bio').prefetch_related('bio__books')

Thanks Aymeric Augustin for the report and tests. Refs #17001.
This commit is contained in:
Loic Bistuer
2014-05-19 03:43:13 +07:00
parent 8a95b4fca7
commit bdf3473e64
3 changed files with 77 additions and 11 deletions

View File

@@ -1790,15 +1790,6 @@ def prefetch_related_objects(result_cache, related_lookups):
done_queries[prefetch_to] = obj_list
auto_lookups.extend(normalize_prefetch_lookups(additional_lookups, prefetch_to))
followed_descriptors.add(descriptor)
elif isinstance(getattr(first_obj, through_attr), list):
# The current part of the lookup relates to a custom Prefetch.
# This means that obj.attr is a list of related objects, and
# thus we must turn the obj.attr lists into a single related
# object list.
new_list = []
for obj in obj_list:
new_list.extend(getattr(obj, through_attr))
obj_list = new_list
else:
# Either a singly related object that has already been fetched
# (e.g. via select_related), or hopefully some other property
@@ -1815,7 +1806,13 @@ def prefetch_related_objects(result_cache, related_lookups):
continue
if new_obj is None:
continue
new_obj_list.append(new_obj)
# We special-case `list` rather than something more generic
# like `Iterable` because we don't want to accidentally match
# user models that define __iter__.
if isinstance(new_obj, list):
new_obj_list.extend(new_obj)
else:
new_obj_list.append(new_obj)
obj_list = new_obj_list