MCPcopy
hub / github.com/ormar-orm/ormar / upsert

Method upsert

ormar/models/model.py:67–89  ·  view source on GitHub ↗

Performs either a save or an update depending on the presence of the pk. If the pk field is filled it's an update, otherwise the save is performed. For save kwargs are ignored, used only in update if provided. :param kwargs: list of fields to update :type kw

(self: T, **kwargs: Any)

Source from the content-addressed store, hash-verified

65 await getattr(cfg.signals, name).send(sender=ancestor, **kwargs)
66
67 async def upsert(self: T, **kwargs: Any) -> T:
68 """
69 Performs either a save or an update depending on the presence of the pk.
70 If the pk field is filled it's an update, otherwise the save is performed.
71 For save kwargs are ignored, used only in update if provided.
72
73 :param kwargs: list of fields to update
74 :type kwargs: Any
75 :return: saved Model
76 :rtype: Model
77 """
78
79 force_save = kwargs.pop("__force_save__", False)
80 if force_save:
81 expr = self.ormar_config.table.select().where(self.pk_column == self.pk)
82 row = await self._execute_query(expr, is_select=True)
83 if not row:
84 return await self.save()
85 return await self.update(**kwargs)
86
87 if not self.pk:
88 return await self.save()
89 return await self.update(**kwargs)
90
91 async def save(self: T) -> T:
92 """

Callers 7

test_bulk_methodsFunction · 0.80
create_countryFunction · 0.80
create_selfrefFunction · 0.80
test_onupdate_upsertFunction · 0.80
addMethod · 0.80
_upsert_modelMethod · 0.80

Calls 4

_execute_queryMethod · 0.95
saveMethod · 0.95
updateMethod · 0.95
popMethod · 0.80

Tested by 5

test_bulk_methodsFunction · 0.64
create_countryFunction · 0.64
create_selfrefFunction · 0.64
test_onupdate_upsertFunction · 0.64