Fix N+1 translation cache lookups in celery translation tasks#2731
Open
szabozoltan69 wants to merge 1 commit intodevelopfrom
Open
Fix N+1 translation cache lookups in celery translation tasks#2731szabozoltan69 wants to merge 1 commit intodevelopfrom
szabozoltan69 wants to merge 1 commit intodevelopfrom
Conversation
thenav56
requested changes
May 3, 2026
Member
thenav56
left a comment
There was a problem hiding this comment.
Thanks @szabozoltan69 for integrating with cache table.
Member
There was a problem hiding this comment.
Maybe we can refactor the whole function with the new cache logic.
Something like this
def translate_fields_object(self, obj, field):
initial_lang = getattr(obj, TRANSLATOR_ORIGINAL_LANGUAGE_FIELD_NAME)
initial_value = getattr(obj, build_localized_fieldname(field, initial_lang), None)
if not initial_value or not initial_lang:
return
model = type(obj)
table_field = f"{model._meta.app_label}:{model._meta.model_name}:{field}"
field_max_length = model._meta.get_field(field).max_length
# Determine which languages still need translation
pending_langs = {
lang: build_localized_fieldname(field, lang)
for lang in AVAILABLE_LANGUAGES
if not getattr(obj, build_localized_fieldname(field, lang), None)
}
if not pending_langs:
return
# Batch-fetch cached translations for all pending languages at once
cached = self.translator.get_cached_translations(
initial_value,
list(pending_langs.keys()),
source_language=initial_lang,
table_field=table_field,
)
for lang, lang_field in pending_langs.items():
translated = cached.get(lang) or self.translator.translate_text(
initial_value,
lang,
source_language=initial_lang,
table_field=table_field,
)
if translated is None:
logger.warning(
"Translation failed for %s.%s pk=%s",
model.__name__,
lang_field,
obj.pk,
)
continue
if field_max_length and len(translated) > field_max_length:
logger.warning(
"Translation exceeds max_length (%d) for %s.%s pk=%s",
field_max_length, model.__name__, lang_field, obj.pk,
)
translated = translated[:field_max_length]
setattr(obj, lang_field, translated)
yield lang_fieldI haven't tested this.
Comment on lines
+217
to
+220
| if not caches: | ||
| return {} | ||
|
|
||
| cache_by_lang = {cache.dest_language: cache for cache in caches} |
Member
There was a problem hiding this comment.
if not caches will be always be true as caches is a queryset object.
We can instead use cache_by_lang for if
Suggested change
| if not caches: | |
| return {} | |
| cache_by_lang = {cache.dest_language: cache for cache in caches} | |
| cache_by_lang = {cache.dest_language: cache for cache in caches} | |
| if not cache_by_lang: | |
| return {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For Sentry issue /organizations/ifrc-go/issues/5764