<s> """<STR_LIT>""" <EOL> def patch_prefetch_one_level ( ) : <EOL> """<STR_LIT>""" <EOL> import copy <EOL> def prefetch_one_level ( instances , prefetcher , lookup , level ) : <EOL> rel_qs , rel_obj_attr , instance_attr , single , cache_name = ( <EOL> prefetcher . get_prefetch_queryset ( <EOL> instances , lookup . get_current_queryset ( level ) ) ) <EOL> additional_lookups = [ <EOL> copy . copy ( additional_lookup ) for additional_lookup <EOL> in getattr ( rel_qs , '<STR_LIT>' , [ ] ) <EOL> ] <EOL> if additional_lookups : <EOL> rel_qs . _prefetch_related_lookups = [ ] <EOL> all_related_objects = list ( rel_qs ) <EOL> rel_obj_cache = { } <EOL> for rel_obj in all_related_objects : <EOL> rel_attr_val = rel_obj_attr ( rel_obj ) <EOL> rel_obj_cache . setdefault ( rel_attr_val , [ ] ) . append ( rel_obj ) <EOL> for obj in instances : <EOL> instance_attr_val = instance_attr ( obj ) <EOL> vals = rel_obj_cache . get ( instance_attr_val , [ ] ) <EOL> to_attr , as_attr = lookup . get_current_to_attr ( level ) <EOL> if single : <EOL> val = vals [ <NUM_LIT:0> ] if vals else None <EOL> to_attr = to_attr if as_attr else cache_name <EOL> setattr ( obj , to_attr , val ) <EOL> else : <EOL> if as_attr : <EOL> setattr ( obj , to_attr , vals ) <EOL> else : <EOL> qs = getattr ( obj , to_attr ) . all ( ) <EOL> qs . _result_cache = vals <EOL> qs . _prefetch_done = True <EOL> obj . _prefetched_objects_cache [ cache_name ] = qs <EOL> return all_related_objects , additional_lookups <EOL> from django . db . models import query <EOL> query . prefetch_one_level = prefetch_one_level </s>
