def save(self):
        self._run_callbacks('before_save')

        fields_dict = self.fields.as_dict()
        try:
            # Attempt update
            id_ = fields_dict['id']
            result = (r.table(self._table).get(id_).replace(r.row
                        .without(r.row.keys().difference(list(fields_dict.keys())))
                        .merge(fields_dict), return_changes='always').run())

        except __HOLE__:
            # Resort to insert
            result = (r.table(self._table).insert(fields_dict, return_changes=True)
                      .run())

        if result['errors'] > 0:
            raise OperationError(result['first_error'])

        # Force overwrite so that related caches are flushed
        self.fields.__dict__ = result['changes'][0]['new_val']

        self._run_callbacks('after_save')
dataset/ETHPy150Open linkyndy/remodel/remodel/models.py/Model.save