Performs update of Model instance in the database. Fields can be updated before or you can pass them as kwargs. Sends pre_update and post_update signals. Sets model save status to True. :param _columns: list of columns to update, if None all are updated
(self: T, _columns: Optional[list[str]] = None, **kwargs: Any)
| 271 | return update_count |
| 272 | |
| 273 | async def update(self: T, _columns: Optional[list[str]] = None, **kwargs: Any) -> T: |
| 274 | """ |
| 275 | Performs update of Model instance in the database. |
| 276 | Fields can be updated before or you can pass them as kwargs. |
| 277 | |
| 278 | Sends pre_update and post_update signals. |
| 279 | |
| 280 | Sets model save status to True. |
| 281 | |
| 282 | :param _columns: list of columns to update, if None all are updated |
| 283 | :type _columns: list |
| 284 | :raises ModelPersistenceError: If the pk column is not set |
| 285 | |
| 286 | :param kwargs: list of fields to update as field=value pairs |
| 287 | :type kwargs: Any |
| 288 | :return: updated Model |
| 289 | :rtype: Model |
| 290 | """ |
| 291 | explicit_fields = self.__setattr_fields__ | kwargs.keys() |
| 292 | values = self.populate_onupdate_value( |
| 293 | dict(kwargs), explicit_fields=explicit_fields |
| 294 | ) |
| 295 | if values: |
| 296 | self.update_from_dict(values) |
| 297 | |
| 298 | if not self.pk: |
| 299 | raise ModelPersistenceError( |
| 300 | "You cannot update not saved model! Use save or upsert method." |
| 301 | ) |
| 302 | |
| 303 | await self._emit_signal("pre_update", instance=self, passed_args=kwargs) |
| 304 | self_fields = self._extract_model_db_fields() |
| 305 | self_fields.pop(self.get_column_name_from_alias(self.ormar_config.pkname)) |
| 306 | if _columns: |
| 307 | self_fields = { |
| 308 | k: v |
| 309 | for k, v in self_fields.items() |
| 310 | if k in _columns or k in self._onupdate_fields |
| 311 | } |
| 312 | if self_fields: |
| 313 | self_fields = self.translate_columns_to_aliases(self_fields) |
| 314 | expr = self.ormar_config.table.update().values(**self_fields) |
| 315 | expr = expr.where(self.pk_column == getattr(self, self.ormar_config.pkname)) |
| 316 | await self._execute_query(expr) |
| 317 | self.set_save_status(True) |
| 318 | self.__setattr_fields__.clear() |
| 319 | await self._emit_signal("post_update", instance=self) |
| 320 | return self |
| 321 | |
| 322 | async def delete(self) -> int: |
| 323 | """ |