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_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)