Skip to content

Commit 5ee651f

Browse files
Fixed #36369 -- Cleared additional cached properties in apps.clear_cache().
Thanks Clifford Gama for the report. Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
1 parent fd70591 commit 5ee651f

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

django/db/models/options.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ class Options:
100100
"managers_map",
101101
"base_manager",
102102
"default_manager",
103+
"db_returning_fields",
104+
"_property_names",
105+
"pk_fields",
106+
"total_unique_constraints",
107+
"all_parents",
108+
"swapped",
109+
"verbose_name_raw",
103110
}
104111
REVERSE_PROPERTIES = {"related_objects", "fields_map", "_relation_tree"}
105112

tests/apps/tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
skipUnlessDBFeature,
1515
)
1616
from django.test.utils import extend_sys_path, isolate_apps
17+
from django.utils.functional import cached_property
1718

1819
from .models import SoAlternative, TotallyNormal, new_apps
1920
from .one_config_app.apps import OneConfig
@@ -215,6 +216,27 @@ def test_clear_cache(self):
215216
self.assertEqual(apps.get_swappable_settings_name.cache_info().currsize, 0)
216217
self.assertEqual(apps.get_models.cache_info().currsize, 0)
217218

219+
@override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
220+
def test_cached_properties_cleared_after_cache_clear(self):
221+
opts = apps.get_model("admin", "LogEntry")._meta
222+
223+
cached_properties = [
224+
name
225+
for name, attr in models.options.Options.__dict__.items()
226+
if isinstance(attr, cached_property)
227+
]
228+
229+
# Access each cached property to populate the cache.
230+
for attr_name in cached_properties:
231+
getattr(opts, attr_name)
232+
self.assertIn(attr_name, opts.__dict__)
233+
234+
apps.clear_cache()
235+
236+
for attr_name in cached_properties:
237+
with self.subTest(property=attr_name):
238+
self.assertNotIn(attr_name, opts.__dict__)
239+
218240
@override_settings(INSTALLED_APPS=["apps.apps.RelabeledAppsConfig"])
219241
def test_relabeling(self):
220242
self.assertEqual(apps.get_app_config("relabeled").name, "apps")

0 commit comments

Comments
 (0)