Try to update the model. Return a list of updated fields if the model was updated (if an update query was done and a matching row was found in the DB).
(
self,
base_qs,
using,
pk_val,
values,
update_fields,
forced_update,
returning_fields,
)
| 1196 | return updated |
| 1197 | |
| 1198 | def _do_update( |
| 1199 | self, |
| 1200 | base_qs, |
| 1201 | using, |
| 1202 | pk_val, |
| 1203 | values, |
| 1204 | update_fields, |
| 1205 | forced_update, |
| 1206 | returning_fields, |
| 1207 | ): |
| 1208 | """ |
| 1209 | Try to update the model. Return a list of updated fields if the model |
| 1210 | was updated (if an update query was done and a matching row was |
| 1211 | found in the DB). |
| 1212 | """ |
| 1213 | filtered = base_qs.filter(pk=pk_val) |
| 1214 | if not values: |
| 1215 | # We can end up here when saving a model in inheritance chain where |
| 1216 | # update_fields doesn't target any field in current model. In that |
| 1217 | # case we just say the update succeeded. Another case ending up |
| 1218 | # here is a model with just PK - in that case check that the PK |
| 1219 | # still exists. |
| 1220 | if update_fields is not None or filtered.exists(): |
| 1221 | return [()] |
| 1222 | return [] |
| 1223 | if self._meta.select_on_save and not forced_update: |
| 1224 | # It may happen that the object is deleted from the DB right after |
| 1225 | # this check, causing the subsequent UPDATE to return zero matching |
| 1226 | # rows. The same result can occur in some rare cases when the |
| 1227 | # database returns zero despite the UPDATE being executed |
| 1228 | # successfully (a row is matched and updated). In order to |
| 1229 | # distinguish these two cases, the object's existence in the |
| 1230 | # database is again checked for if the UPDATE query returns 0. |
| 1231 | if not filtered.exists(): |
| 1232 | return [] |
| 1233 | if results := filtered._update(values, returning_fields): |
| 1234 | return results |
| 1235 | return [()] if filtered.exists() else [] |
| 1236 | return filtered._update(values, returning_fields) |
| 1237 | |
| 1238 | def _do_insert(self, manager, using, fields, returning_fields, raw): |
| 1239 | """ |
no test coverage detected