<s> from collections import defaultdict <EOL> from django . db . models . query import QuerySet <EOL> def group_connections ( connections ) : <EOL> """<STR_LIT>""" <EOL> grouped_conns = defaultdict ( list ) <EOL> if isinstance ( connections , QuerySet ) : <EOL> languages = connections . values_list ( '<STR_LIT>' , flat = True ) <EOL> for language in languages . distinct ( ) : <EOL> lang_conns = connections . filter ( contact__language = language ) <EOL> grouped_conns [ language ] . extend ( lang_conns ) <EOL> else : <EOL> for connection in connections : <EOL> language = connection . contact . language <EOL> grouped_conns [ language ] . append ( connection ) <EOL> for lang , conns in grouped_conns . items ( ) : <EOL> yield lang , conns </s>
