88from functools import partialmethod
99from functools import wraps
1010
11+ from django import VERSION as DJANGO_VERSION
1112from django .apps import apps as django_apps
1213from django .db import models
1314from django .db .models import Field
@@ -479,7 +480,7 @@ def __init__(self, *args, **kwargs):
479480 def state_fields (self ):
480481 return filter (lambda field : isinstance (field , FSMFieldMixin ), self ._meta .fields )
481482
482- def _do_update (self , base_qs , using , pk_val , values , update_fields , forced_update ):
483+ def _do_update (self , base_qs , using , pk_val , values , update_fields , forced_update , returning_fields = None ):
483484 # _do_update is called once for each model class in the inheritance hierarchy.
484485 # We can only filter the base_qs on state fields (can be more than one!) present in this particular model.
485486
@@ -489,14 +490,26 @@ def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_updat
489490 # state filter will be used to narrow down the standard filter checking only PK
490491 state_filter = {field .attname : self .__initial_states [field .attname ] for field in filter_on }
491492
492- updated = super ()._do_update (
493- base_qs = base_qs .filter (** state_filter ),
494- using = using ,
495- pk_val = pk_val ,
496- values = values ,
497- update_fields = update_fields ,
498- forced_update = forced_update ,
499- )
493+ # Django 6.0+ added returning_fields parameter to _do_update
494+ if DJANGO_VERSION >= (6 , 0 ):
495+ updated = super ()._do_update (
496+ base_qs = base_qs .filter (** state_filter ),
497+ using = using ,
498+ pk_val = pk_val ,
499+ values = values ,
500+ update_fields = update_fields ,
501+ forced_update = forced_update ,
502+ returning_fields = returning_fields ,
503+ )
504+ else :
505+ updated = super ()._do_update (
506+ base_qs = base_qs .filter (** state_filter ),
507+ using = using ,
508+ pk_val = pk_val ,
509+ values = values ,
510+ update_fields = update_fields ,
511+ forced_update = forced_update ,
512+ )
500513
501514 # It may happen that nothing was updated in the original _do_update method not because of unmatching state,
502515 # but because of missing PK. This codepath is possible when saving a new model instance with *preset PK*.
0 commit comments