

def get_prefetch_query_set(self, instances):
    rel_obj_attr = attrgetter(self.related.field.attname)
    instance_attr = (lambda obj: obj._get_pk_val())
    instances_dict = dict(((instance_attr(inst), inst) for inst in instances))
    params = {
        ('%s__pk__in' % self.related.field.name): list(instances_dict),
    }
    qs = self.get_query_set(instance=instances[0]).filter(**params)
    rel_obj_cache_name = self.related.field.get_cache_name()
    for rel_obj in qs:
        instance = instances_dict[rel_obj_attr(rel_obj)]
        setattr(rel_obj, rel_obj_cache_name, instance)
    return (qs, rel_obj_attr, instance_attr, True, self.cache_name)

dataset/ETHPy150Open AppScale/appscale/AppServer/lib/django-1.5/django/db/models/fields/related.py SingleRelatedObjectDescriptor.get_prefetch_query_set/original