def prefetch_one_level(instances, prefetcher, attname):
 (rel_qs, rel_obj_attr, instance_attr, single, cache_name) = prefetcher.get_prefetch_queryset(instances)
 additional_prl = getattr(rel_qs, '_prefetch_related_lookups', [])
 if additional_prl:
  rel_qs._prefetch_related_lookups = []
 all_related_objects = list(rel_qs)
 rel_obj_cache = {}
 for rel_obj in all_related_objects:
  rel_attr_val = rel_obj_attr(rel_obj)
  rel_obj_cache.setdefault(rel_attr_val, []).append(rel_obj)
 for obj in instances:
  instance_attr_val = instance_attr(obj)
  vals = rel_obj_cache.get(instance_attr_val, [])
  if single:
   setattr(obj, cache_name, (vals[0] if vals else None))
  else:
   qs = getattr(obj, attname).all()
   qs._result_cache = vals
   qs._prefetch_done = True
   obj._prefetched_objects_cache[cache_name] = qs
 return (all_related_objects, additional_prl)

def prefetch_one_level ( instances , prefetcher , attname ) : ( rel_qs , rel_obj_attr , instance_attr , single , cache_name ) = prefetcher . get_prefetch_queryset ( instances ) additional_prl = getattr ( rel_qs , '_prefetch_related_lookups' , [ ] ) if additional_prl : rel_qs . _prefetch_related_lookups = [ ] all_related_objects = list ( rel_qs ) rel_obj_cache = { } for rel_obj in all_related_objects : rel_attr_val = rel_obj_attr ( rel_obj ) rel_obj_cache . setdefault ( rel_attr_val , [ ] ) . append ( rel_obj ) for obj in instances : instance_attr_val = instance_attr ( obj ) vals = rel_obj_cache . get ( instance_attr_val , [ ] ) if single : setattr ( obj , cache_name , ( vals [ 0 ] if vals else None ) ) else : qs = getattr ( obj , attname ) . all ( ) qs . _result_cache = vals qs . _prefetch_done = True obj . _prefetched_objects_cache [ cache_name ] = qs return ( all_related_objects , additional_prl )
helper function for prefetch_related_objects runs prefetches on all instances using the prefetcher object .