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