@@ -1094,12 +1094,28 @@ def _save_table(
10941094 ]
10951095 forced_update = update_fields or force_update
10961096 pk_val = self ._get_pk_val (meta )
1097- updated = self ._do_update (
1098- base_qs , using , pk_val , values , update_fields , forced_update
1097+ returning_fields = [
1098+ f
1099+ for f in meta .local_concrete_fields
1100+ if (
1101+ f .generated
1102+ and f .referenced_fields .intersection (non_pks_non_generated )
1103+ )
1104+ ]
1105+ results = self ._do_update (
1106+ base_qs ,
1107+ using ,
1108+ pk_val ,
1109+ values ,
1110+ update_fields ,
1111+ forced_update ,
1112+ returning_fields ,
10991113 )
1100- if force_update and not updated :
1114+ if updated := bool (results ):
1115+ self ._assign_returned_values (results [0 ], returning_fields )
1116+ elif force_update :
11011117 raise self .NotUpdated ("Forced update did not affect any rows." )
1102- if update_fields and not updated :
1118+ elif update_fields :
11031119 raise self .NotUpdated (
11041120 "Save with update_fields did not affect any rows."
11051121 )
@@ -1131,11 +1147,19 @@ def _save_table(
11311147 cls ._base_manager , using , fields , returning_fields , raw
11321148 )
11331149 if results :
1134- for value , field in zip (results [0 ], returning_fields ):
1135- setattr (self , field .attname , value )
1150+ self ._assign_returned_values (results [0 ], returning_fields )
11361151 return updated
11371152
1138- def _do_update (self , base_qs , using , pk_val , values , update_fields , forced_update ):
1153+ def _do_update (
1154+ self ,
1155+ base_qs ,
1156+ using ,
1157+ pk_val ,
1158+ values ,
1159+ update_fields ,
1160+ forced_update ,
1161+ returning_fields ,
1162+ ):
11391163 """
11401164 Try to update the model. Return True if the model was updated (if an
11411165 update query was done and a matching row was found in the DB).
@@ -1147,22 +1171,23 @@ def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_updat
11471171 # case we just say the update succeeded. Another case ending up
11481172 # here is a model with just PK - in that case check that the PK
11491173 # still exists.
1150- return update_fields is not None or filtered .exists ()
1174+ if update_fields is not None or filtered .exists ():
1175+ return [()]
1176+ return []
11511177 if self ._meta .select_on_save and not forced_update :
1152- return (
1153- filtered .exists ()
1154- and
1155- # It may happen that the object is deleted from the DB right
1156- # after this check, causing the subsequent UPDATE to return
1157- # zero matching rows. The same result can occur in some rare
1158- # cases when the database returns zero despite the UPDATE being
1159- # executed successfully (a row is matched and updated). In
1160- # order to distinguish these two cases, the object's existence
1161- # in the database is again checked for if the UPDATE query
1162- # returns 0.
1163- (filtered ._update (values ) > 0 or filtered .exists ())
1164- )
1165- return filtered ._update (values ) > 0
1178+ # It may happen that the object is deleted from the DB right after
1179+ # this check, causing the subsequent UPDATE to return zero matching
1180+ # rows. The same result can occur in some rare cases when the
1181+ # database returns zero despite the UPDATE being executed
1182+ # successfully (a row is matched and updated). In order to
1183+ # distinguish these two cases, the object's existence in the
1184+ # database is again checked for if the UPDATE query returns 0.
1185+ if not filtered .exists ():
1186+ return []
1187+ if results := filtered ._update (values , returning_fields ):
1188+ return results
1189+ return [()] if filtered .exists () else []
1190+ return filtered ._update (values , returning_fields )
11661191
11671192 def _do_insert (self , manager , using , fields , returning_fields , raw ):
11681193 """
@@ -1177,6 +1202,10 @@ def _do_insert(self, manager, using, fields, returning_fields, raw):
11771202 raw = raw ,
11781203 )
11791204
1205+ def _assign_returned_values (self , returned_values , returning_fields ):
1206+ for value , field in zip (returned_values , returning_fields ):
1207+ setattr (self , field .attname , value )
1208+
11801209 def _prepare_related_fields_for_save (self , operation_name , fields = None ):
11811210 # Ensure that a model instance without a PK hasn't been assigned to
11821211 # a ForeignKey, GenericForeignKey or OneToOneField on this model. If
0 commit comments