def _update_or_create(cls, malt_dict): try: # try update unique_field_dict = { k: v for (k, v) in malt_dict.iteritems() if k in MALTRow.get_unique_fields() } prev_obj = MALTRow.objects.get(**unique_field_dict) for k, v in malt_dict.iteritems(): setattr(prev_obj, k, v) prev_obj.save() except MALTRow.DoesNotExist: # create try: MALTRow(**malt_dict).save() except Exception as ex: logger.error( "Failed to insert malt-row {}. Exception is {}".format( str(malt_dict), str(ex)), exc_info=True) except Exception as ex: logger.error( "Failed to insert malt-row {}. Exception is {}".format( str(malt_dict), str(ex)), exc_info=True)
def _save_malt_row_dicts_to_db(malt_row_dicts): try: MALTRow.objects.bulk_create( [MALTRow(**malt_dict) for malt_dict in malt_row_dicts] ) except IntegrityError: for malt_row_dict in malt_row_dicts: _update_or_create_malt_row(malt_row_dict)
def _save_to_db(cls, malt_rows_to_save, domain_id): try: MALTRow.objects.bulk_create( [MALTRow(**malt_dict) for malt_dict in malt_rows_to_save] ) except IntegrityError: # no update_or_create in django-1.6 for malt_dict in malt_rows_to_save: cls._update_or_create(malt_dict) except Exception as ex: logger.error("Failed to insert rows for domain with id {id}. Exception is {ex}".format( id=domain_id, ex=str(ex)), exc_info=True)
def handle(self, *args, **options): for arg in args: with open(arg, 'r') as file: rows = [] reader = csv.reader(file) header_row = True for row in reader: if header_row: headers = row header_row = False else: rows.append({headers[index]: item for index, item in enumerate(row)}) MALTRow.objects.bulk_create( [MALTRow(**malt_dict) for malt_dict in rows] )
def _save_to_db(cls, malt_rows_to_save, domain_id): try: MALTRow.objects.bulk_create( [MALTRow(**malt_dict) for malt_dict in malt_rows_to_save] ) except IntegrityError: # no update_or_create in django-1.6 for malt_dict in malt_rows_to_save: try: unique_field_dict = {k: v for (k, v) in malt_dict.iteritems() if k in MALTRow.get_unique_fields()} prev_obj = MALTRow.objects.get(**unique_field_dict) for k, v in malt_dict.iteritems(): setattr(prev_obj, k, v) prev_obj.save() except MALTRow.DoesNotExist: MALTRow(**malt_dict).save() except Exception as ex: logger.info("Failed to insert rows for domain with id {id}. Exception is {ex}".format( id=domain_id, ex=str(ex)))
def _update_or_create(cls, malt_dict): try: # try update unique_field_dict = {k: v for (k, v) in malt_dict.iteritems() if k in MALTRow.get_unique_fields()} prev_obj = MALTRow.objects.get(**unique_field_dict) for k, v in malt_dict.iteritems(): setattr(prev_obj, k, v) prev_obj.save() except MALTRow.DoesNotExist: # create try: MALTRow(**malt_dict).save() except Exception as ex: logger.error("Failed to insert malt-row {}. Exception is {}".format( str(malt_dict), str(ex) ), exc_info=True) except Exception as ex: logger.error("Failed to insert malt-row {}. Exception is {}".format( str(malt_dict), str(ex) ), exc_info=True)
def _update_or_create_malt_row(malt_row_dict): unique_field_dict = {k: v for (k, v) in malt_row_dict.items() if k in MALTRow.get_unique_fields()} # use the unique_field_dict to specify key/value pairs to filter on MALTRow.objects.update_or_create(defaults=malt_row_dict, **unique_field_dict)