def group_models_by_index(backend, models):
 indices = {}
 models_by_index = collections.OrderedDict()
 for model in models:
  index = backend.get_index_for_model(model)
  if index:
   indices.setdefault(index.name, index)
   models_by_index.setdefault(index.name, [])
   models_by_index[index.name].append(model)
 return collections.OrderedDict([(indices[index_name], index_models) for (index_name, index_models) in models_by_index.items()])

def group_models_by_index ( backend , models ) : indices = { } models_by_index = collections . OrderedDict ( ) for model in models : index = backend . get_index_for_model ( model ) if index : indices . setdefault ( index . name , index ) models_by_index . setdefault ( index . name , [ ] ) models_by_index [ index . name ] . append ( model ) return collections . OrderedDict ( [ ( indices [ index_name ] , index_models ) for ( index_name , index_models ) in models_by_index . items ( ) ] )
this takes a search backend and a list of models .