

def get_prefetch_query_set(self, instances):
    other_field = self.field.rel.get_related_field()
    rel_obj_attr = attrgetter(other_field.attname)
    instance_attr = attrgetter(self.field.attname)
    instances_dict = dict(((instance_attr(inst), inst) for inst in instances))
    if other_field.rel:
        params = {
            ('%s__pk__in' % self.field.rel.field_name): list(instances_dict),
        }
    else:
        params = {
            ('%s__in' % self.field.rel.field_name): list(instances_dict),
        }
    qs = self.get_query_set(instance=instances[0]).filter(**params)
    if (not self.field.rel.multiple):
        rel_obj_cache_name = self.field.related.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 ReverseSingleRelatedObjectDescriptor.get_prefetch_query_set/original