Creates/Updates the current model object. :param update_fields: If provided, it should be a tuple/list of fields by name. This is the subset of fields that should be updated. If the object needs to be created ``update_fields`` will be ignored. :para
(
self,
using_db: BaseDBAsyncClient | None = None,
update_fields: Iterable[str] | None = None,
force_create: bool = False,
force_update: bool = False,
)
| 1128 | await self._wait_for_listeners(Signals.post_save, created, using_db, update_fields) |
| 1129 | |
| 1130 | async def save( |
| 1131 | self, |
| 1132 | using_db: BaseDBAsyncClient | None = None, |
| 1133 | update_fields: Iterable[str] | None = None, |
| 1134 | force_create: bool = False, |
| 1135 | force_update: bool = False, |
| 1136 | ) -> None: |
| 1137 | """ |
| 1138 | Creates/Updates the current model object. |
| 1139 | |
| 1140 | :param update_fields: If provided, it should be a tuple/list of fields by name. |
| 1141 | |
| 1142 | This is the subset of fields that should be updated. |
| 1143 | If the object needs to be created ``update_fields`` will be ignored. |
| 1144 | :param using_db: Specific DB connection to use instead of default bound |
| 1145 | :param force_create: Forces creation of the record |
| 1146 | :param force_update: Forces updating of the record |
| 1147 | |
| 1148 | :raises IncompleteInstanceError: If the model is partial and the fields are not available for persistence. |
| 1149 | :raises IntegrityError: If the model can't be created or updated (specifically if force_create or force_update has been set) |
| 1150 | :raises OperationalError: If update_fields include pk field. |
| 1151 | """ |
| 1152 | await self._set_async_default_field() |
| 1153 | db = using_db or self._choose_db(True) |
| 1154 | executor = db.executor_class(model=self.__class__, db=db) |
| 1155 | if self._partial: |
| 1156 | if update_fields: |
| 1157 | for field in update_fields: |
| 1158 | if not hasattr(self, self._meta.pk_attr): |
| 1159 | raise IncompleteInstanceError( |
| 1160 | f"{self.__class__.__name__} is a partial model without primary key fetchd. Partial update not available" |
| 1161 | ) |
| 1162 | if not hasattr(self, field): |
| 1163 | raise IncompleteInstanceError( |
| 1164 | f"{self.__class__.__name__} is a partial model, field '{field}' is not available" |
| 1165 | ) |
| 1166 | else: |
| 1167 | raise IncompleteInstanceError( |
| 1168 | f"{self.__class__.__name__} is a partial model, can only be saved with the relevant update_field provided" |
| 1169 | ) |
| 1170 | await self._pre_save(db, update_fields) |
| 1171 | |
| 1172 | if force_create: |
| 1173 | await executor.execute_insert(self) |
| 1174 | created = True |
| 1175 | elif force_update: |
| 1176 | rows = await executor.execute_update(self, update_fields) |
| 1177 | if rows == 0: |
| 1178 | raise IntegrityError(f"Can't update object that doesn't exist. PK: {self.pk}") |
| 1179 | created = False |
| 1180 | else: |
| 1181 | if self._saved_in_db or update_fields: |
| 1182 | if self.pk is None: |
| 1183 | await executor.execute_insert(self) |
| 1184 | created = True |
| 1185 | else: |
| 1186 | await executor.execute_update(self, update_fields) |
| 1187 | created = False |