diff --git a/django/db/models/query.py b/django/db/models/query.py
index b62b197..9b22e9e 100644
a
|
b
|
def _prefetch_one_level(instances, relmanager, attname):
|
1642 | 1642 | if additional_prf: |
1643 | 1643 | rel_qs = rel_qs.prefetch_related(None) |
1644 | 1644 | all_related_objects = list(rel_qs) |
| 1645 | |
| 1646 | rel_obj_cache = {} |
| 1647 | for rel_obj in all_related_objects: |
| 1648 | rel_attr_val = getattr(rel_obj, rel_obj_attr) |
| 1649 | if rel_attr_val not in rel_obj_cache: |
| 1650 | rel_obj_cache[rel_attr_val] = [] |
| 1651 | rel_obj_cache[rel_attr_val].append(rel_obj) |
| 1652 | |
1645 | 1653 | for obj in instances: |
1646 | 1654 | qs = getattr(obj, attname).all() |
1647 | 1655 | instance_attr_val = getattr(obj, instance_attr) |
1648 | | qs._result_cache = [rel_obj for rel_obj in all_related_objects |
1649 | | if getattr(rel_obj, rel_obj_attr) == instance_attr_val] |
| 1656 | qs._result_cache = rel_obj_cache.get(instance_attr_val, []) |
1650 | 1657 | # We don't want the individual qs doing prefetch_related now, since we |
1651 | 1658 | # have merged this into the current work. |
1652 | 1659 | qs._prefetch_done = True |