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

Query database for translated content if translation was cached as missing #1

Merged
merged 1 commit into from
Apr 25, 2023
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
15 changes: 11 additions & 4 deletions parler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,9 @@ def _get_translated_model(
local_cache[language_code] = MISSING # Set fallback marker
local_cache[object.language_code] = object
return object
elif is_missing(local_cache.get(language_code, None)):
# If get_cached_translation() explicitly set the "does not exist" marker,
# there is no need to try a database query.
pass
else:
model_is_missing = is_missing(local_cache.get(language_code, None))

# 2.3, fetch from database
try:
object = self._get_translated_queryset(meta).get(
Expand All @@ -576,6 +574,15 @@ def _get_translated_model(
else:
local_cache[language_code] = object
_cache_translation(object) # Store in memcached

if model_is_missing:
msg = f"""
{self._meta.verbose_name} ID {self.pk} was cached as missing but
existed in the database. This was corrected but introduced an
additional query.
"""
warnings.warn(msg)

return object

# Not in cache, or default.
Expand Down
14 changes: 14 additions & 0 deletions parler/tests/test_query_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from parler import appsettings
from parler.cache import get_translation_cache_key
from parler.cache import _cache_translation_needs_fallback

from .testapp.models import DateTimeModel, SimpleModel
from .utils import AppTestCase, override_parler_settings
Expand Down Expand Up @@ -125,3 +126,16 @@ def test_get_translation_cache_key_with_prefix(self):
field = model.translations.first()
key = get_translation_cache_key(field.__class__, 1, "en")
self.assertEqual(key, "mysite.parler.testapp.SimpleModelTranslation.1.en")

def test_get_translation_not_in_cache(self):
"""
The default "missing" behaviour was modified as we saw translations being cached as
missing when they didn't miss.
"""
cache.clear()

with override_parler_settings(PARLER_ENABLE_CACHING=True):
model = SimpleModel.objects.first()
_cache_translation_needs_fallback(model, model.get_current_language(), model._parler_meta.root.rel_name)
str(model.tr_title)