Ticket #16937: 16937_dict_join.diff

File 16937_dict_join.diff, 1.1 KB (added by Anssi Kääriäinen, 13 years ago)
  • django/db/models/query.py

    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):  
    16421642    if additional_prf:
    16431643        rel_qs = rel_qs.prefetch_related(None)
    16441644    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
    16451653    for obj in instances:
    16461654        qs = getattr(obj, attname).all()
    16471655        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, [])
    16501657        # We don't want the individual qs doing prefetch_related now, since we
    16511658        # have merged this into the current work.
    16521659        qs._prefetch_done = True
Back to Top