8
8
from functools import partialmethod
9
9
from functools import wraps
10
10
11
+ from django import VERSION as DJANGO_VERSION
11
12
from django .apps import apps as django_apps
12
13
from django .db import models
13
14
from django .db .models import Field
@@ -479,7 +480,7 @@ def __init__(self, *args, **kwargs):
479
480
def state_fields (self ):
480
481
return filter (lambda field : isinstance (field , FSMFieldMixin ), self ._meta .fields )
481
482
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 ):
483
484
# _do_update is called once for each model class in the inheritance hierarchy.
484
485
# We can only filter the base_qs on state fields (can be more than one!) present in this particular model.
485
486
@@ -489,14 +490,26 @@ def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_updat
489
490
# state filter will be used to narrow down the standard filter checking only PK
490
491
state_filter = {field .attname : self .__initial_states [field .attname ] for field in filter_on }
491
492
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
+ )
500
513
501
514
# It may happen that nothing was updated in the original _do_update method not because of unmatching state,
502
515
# but because of missing PK. This codepath is possible when saving a new model instance with *preset PK*.
0 commit comments