File tree 6 files changed +21
-1
lines changed
6 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 1
1
Unreleased
2
2
==========
3
3
4
+ * Fix similarly named models from different apps having the same cache key
5
+
4
6
django-solo-2.3.0
5
7
=================
6
8
Original file line number Diff line number Diff line change @@ -64,7 +64,9 @@ def set_to_cache(self) -> None:
64
64
@classmethod
65
65
def get_cache_key (cls ) -> str :
66
66
prefix = getattr (settings , "SOLO_CACHE_PREFIX" , solo_settings .SOLO_CACHE_PREFIX )
67
- return f"{ prefix } :{ cls .__name__ .lower ()} "
67
+ # Include the model's module in the cache key so similarly named models from different
68
+ # apps do not have the same cache key.
69
+ return f"{ prefix } :{ cls .__module__ .lower ()} :{ cls .__name__ .lower ()} "
68
70
69
71
@classmethod
70
72
def get_solo (cls ) -> Self :
Original file line number Diff line number Diff line change 17
17
INSTALLED_APPS = (
18
18
"solo" ,
19
19
"solo.tests" ,
20
+ "solo.tests.testapp2" ,
20
21
)
21
22
22
23
SECRET_KEY = "any-key"
Original file line number Diff line number Diff line change
1
+ from solo .models import SingletonModel
2
+
3
+
4
+ class SiteConfiguration (SingletonModel ):
5
+ class Meta :
6
+ verbose_name = "Site Configuration 2"
Original file line number Diff line number Diff line change 5
5
from django .test .utils import override_settings
6
6
7
7
from solo .tests .models import SiteConfiguration , SiteConfigurationWithExplicitlyGivenId
8
+ from solo .tests .testapp2 .models import SiteConfiguration as SiteConfiguration2
8
9
9
10
10
11
class SingletonTest (TestCase ):
@@ -103,3 +104,11 @@ def setUp(self):
103
104
def test_when_singleton_instance_id_is_given_created_item_will_have_given_instance_id (self ):
104
105
item = SiteConfigurationWithExplicitlyGivenId .get_solo ()
105
106
self .assertEqual (item .pk , SiteConfigurationWithExplicitlyGivenId .singleton_instance_id )
107
+
108
+
109
+ class SingletonsWithAmbiguousNameTest (TestCase ):
110
+ def test_cache_key_is_not_ambiguous (self ):
111
+ assert SiteConfiguration .get_cache_key () != SiteConfiguration2 .get_cache_key ()
112
+
113
+ def test_get_solo_returns_the_correct_singleton (self ):
114
+ assert SiteConfiguration .get_solo () != SiteConfiguration2 .get_solo ()
You can’t perform that action at this time.
0 commit comments