Skip to content

Commit

Permalink
Fixed #35407 -- Cached model's Options.swapped.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Apr 26, 2024
1 parent ec85524 commit 0511cd8
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion django/db/models/options.py
Expand Up @@ -5,6 +5,7 @@
from django.apps import apps
from django.conf import settings
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
from django.core.signals import setting_changed
from django.db import connections
from django.db.models import AutoField, Manager, OrderWrt, UniqueConstraint
from django.db.models.query_utils import PathInfo
Expand Down Expand Up @@ -230,6 +231,9 @@ def contribute_to_class(self, cls, name):
self.db_table, connection.ops.max_name_length()
)

if self.swappable:
setting_changed.connect(self.setting_changed)

def _format_names(self, objs):
"""App label/class name interpolation for object names."""
names = {"app_label": self.app_label.lower(), "class": self.model_name}
Expand Down Expand Up @@ -399,7 +403,7 @@ def verbose_name_raw(self):
with override(None):
return str(self.verbose_name)

@property
@cached_property
def swapped(self):
"""
Has this model been swapped out for another? If so, return the model
Expand Down Expand Up @@ -427,6 +431,10 @@ def swapped(self):
return swapped_for
return None

def setting_changed(self, *, setting, **kwargs):
if setting == self.swappable and "swapped" in self.__dict__:
del self.swapped

@cached_property
def managers(self):
managers = []
Expand Down

0 comments on commit 0511cd8

Please sign in to comment.