@transaction.atomic
def mass_get_or_create(model_class, base_queryset, id_field, default_dict, global_defaults):
 current_instances = list(base_queryset)
 current_ids = set([unicode(getattr(c, id_field)) for c in current_instances])
 given_ids = map(unicode, default_dict.keys())
 new_ids = [g for g in given_ids if (g not in current_ids)]
 prepared_models = []
 for new_id in new_ids:
  defaults = default_dict[new_id]
  defaults[id_field] = new_id
  defaults.update(global_defaults)
  model_instance = model_class(**defaults)
  prepared_models.append(model_instance)
 if hasattr(model_class.objects, 'bulk_create'):
  model_class.objects.bulk_create(prepared_models)
 else:
  [m.save() for m in prepared_models]
 inserted_model_instances = prepared_models
 return (current_instances, inserted_model_instances)

@ transaction . atomic def mass_get_or_create ( model_class , base_queryset , id_field , default_dict , global_defaults ) : current_instances = list ( base_queryset ) current_ids = set ( [ unicode ( getattr ( c , id_field ) ) for c in current_instances ] ) given_ids = map ( unicode , default_dict . keys ( ) ) new_ids = [ g for g in given_ids if ( g not in current_ids ) ] prepared_models = [ ] for new_id in new_ids : defaults = default_dict [ new_id ] defaults [ id_field ] = new_id defaults . update ( global_defaults ) model_instance = model_class ( ** defaults ) prepared_models . append ( model_instance ) if hasattr ( model_class . objects , 'bulk_create' ) : model_class . objects . bulk_create ( prepared_models ) else : [ m . save ( ) for m in prepared_models ] inserted_model_instances = prepared_models return ( current_instances , inserted_model_instances )
updates the data by inserting all not found records doesnt delete records if not in the new data example usage .