@@ -639,7 +639,7 @@ def update_status_if_checksum_changed(
639639 bool: True if the checksum changed and an update was applied,
640640 False otherwise.
641641 """
642- checksum_changed = self ._should_update_status_based_on_checksum ()
642+ checksum_changed = self ._has_configuration_checksum_changed ()
643643 if checksum_changed :
644644 self .checksum_db = self .checksum
645645 if self .status != "modified" :
@@ -649,18 +649,12 @@ def update_status_if_checksum_changed(
649649 extra_update_fields = ["checksum_db" ],
650650 )
651651 else :
652- # Instead of calling the "save()" method, which would
653- # trigger various signals and checks, we directly update
654- # the "checksum_db" field in the database.
655- self ._meta .model .objects .filter (pk = self .pk ).update (
656- checksum_db = self .checksum_db
657- )
652+ self ._update_checksum_db (new_checksum = self .checksum_db )
658653 return checksum_changed
659654
660- def _should_update_status_based_on_checksum (self ):
655+ def _has_configuration_checksum_changed (self ):
661656 """
662- Determines whether the config status should be updated based on
663- checksum comparison.
657+ Determines whether the config checksum has changed
664658
665659 Returns True if:
666660 - No checksum_db exists (first time)
@@ -675,6 +669,16 @@ def _should_update_status_based_on_checksum(self):
675669 self ._invalidate_backend_instance_cache ()
676670 return self .checksum_db != self .checksum
677671
672+ def _update_checksum_db (self , new_checksum = None ):
673+ """
674+ Updates checksum_db field in the database
675+
676+ It does not call save() to avoid sending signals
677+ and updating other fields.
678+ """
679+ new_checksum = new_checksum or self .checksum
680+ self ._meta .model .objects .filter (pk = self .pk ).update (checksum_db = new_checksum )
681+
678682 def _send_config_modified_signal (self , action ):
679683 """
680684 Emits ``config_modified`` signal.
@@ -780,12 +784,20 @@ def deactivate(self):
780784 """
781785 # Invalidate cached property before checking checksum.
782786 self ._invalidate_backend_instance_cache ()
783- old_checksum = self .checksum
787+ old_checksum = self .checksum_db
788+ # Don't alter the order of the following steps.
789+ # We need to set the status to deactivating before clearing the templates
790+ # otherwise, the "enforce_required_templates" and "add_default_templates"
791+ # methods would re-add required/default templates.
792+ # The "templates_changed" receiver skips post_clear action. Thus,
793+ # we need to update the checksum_db field manually.
784794 self .config = {}
785- self .set_status_deactivating ()
795+ self .set_status_deactivating (save = False )
786796 self .templates .clear ()
787- del self .backend_instance
788- if old_checksum == self .checksum :
797+ self ._invalidate_backend_instance_cache ()
798+ self .checksum_db = self .checksum
799+ self .save ()
800+ if old_checksum == self .checksum_db :
789801 # Accelerate deactivation if the configuration remains
790802 # unchanged (i.e. empty configuration)
791803 self .set_status_deactivated ()
0 commit comments