Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disambiguate cache key for similarly named models #140

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased
==========

* Fix similarly named models from different apps having the same cache key

django-solo-2.3.0
=================

Expand Down
4 changes: 3 additions & 1 deletion solo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def set_to_cache(self) -> None:
@classmethod
def get_cache_key(cls) -> str:
prefix = getattr(settings, "SOLO_CACHE_PREFIX", solo_settings.SOLO_CACHE_PREFIX)
return f"{prefix}:{cls.__name__.lower()}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here that explains why we include cls.__module__?

# Include the model's module in the cache key so similarly named models from different
# apps do not have the same cache key.
return f"{prefix}:{cls.__module__.lower()}:{cls.__name__.lower()}"

@classmethod
def get_solo(cls) -> Self:
Expand Down
1 change: 1 addition & 0 deletions solo/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
INSTALLED_APPS = (
"solo",
"solo.tests",
"solo.tests.testapp2",
)

SECRET_KEY = "any-key"
Expand Down
Empty file added solo/tests/testapp2/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions solo/tests/testapp2/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from solo.models import SingletonModel


class SiteConfiguration(SingletonModel):
class Meta:
verbose_name = "Site Configuration 2"
9 changes: 9 additions & 0 deletions solo/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.test.utils import override_settings

from solo.tests.models import SiteConfiguration, SiteConfigurationWithExplicitlyGivenId
from solo.tests.testapp2.models import SiteConfiguration as SiteConfiguration2


class SingletonTest(TestCase):
Expand Down Expand Up @@ -103,3 +104,11 @@ def setUp(self):
def test_when_singleton_instance_id_is_given_created_item_will_have_given_instance_id(self):
item = SiteConfigurationWithExplicitlyGivenId.get_solo()
self.assertEqual(item.pk, SiteConfigurationWithExplicitlyGivenId.singleton_instance_id)


class SingletonsWithAmbiguousNameTest(TestCase):
def test_cache_key_is_not_ambiguous(self):
assert SiteConfiguration.get_cache_key() != SiteConfiguration2.get_cache_key()

def test_get_solo_returns_the_correct_singleton(self):
assert SiteConfiguration.get_solo() != SiteConfiguration2.get_solo()