#NEWLINE# def prefetch_one_level( instances , prefetcher , attname ) : #NEWLINE# #INDENT# '\n    Helper function for prefetch_related_objects\n\n    Runs prefetches on all instances using the prefetcher object,\n    assigning results to relevant caches in instance.\n\n    The prefetched objects are returned, along with any additional\n    prefetches that must be done due to prefetch_related lookups\n    found from default managers.\n    ' #NEWLINE# ( rel_qs , rel_obj_attr , instance_attr , single , cache_name ) = prefetcher . get_prefetch_query_set ( instances ) #NEWLINE# additional_prl = getattr ( rel_qs , '_prefetch_related_lookups' , [ ] ) #NEWLINE# if additional_prl : #NEWLINE# #INDENT# rel_qs . _prefetch_related_lookups = [ ] #NEWLINE# #UNINDENT# all_related_objects = list ( rel_qs ) #NEWLINE# rel_obj_cache = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for rel_obj in all_related_objects : #NEWLINE# #INDENT# rel_attr_val = rel_obj_attr ( rel_obj ) #NEWLINE# rel_obj_cache . setdefault ( rel_attr_val , [ ] ) . append ( rel_obj ) #NEWLINE# #UNINDENT# for obj in instances : #NEWLINE# #INDENT# instance_attr_val = instance_attr ( obj ) #NEWLINE# vals = rel_obj_cache . get ( instance_attr_val , [ ] ) #NEWLINE# if single : #NEWLINE# #INDENT# setattr ( obj , cache_name , ( vals [ 0 ] if vals else None ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# qs = getattr ( obj , attname ) . all ( ) #NEWLINE# qs . _result_cache = vals #NEWLINE# qs . _prefetch_done = True #NEWLINE# obj . _prefetched_objects_cache [ cache_name ] = qs #NEWLINE# #UNINDENT# #UNINDENT# return ( all_related_objects , additional_prl )