@classmethod
    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)
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/data_analytics/malt_generator.py/MALTTableGenerator._update_or_create/original