Skip to content

Commit

Permalink
feat: add util function to get model instances translations
Browse files Browse the repository at this point in the history
Adds util function which returns a dict of given model's and field's
translations

Refs KER-327
  • Loading branch information
nicobav committed Mar 5, 2024
1 parent 11882ad commit dd1b721
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions democracy/utils/translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.conf import settings


def get_translations_dict(obj, field_name):
"""
Returns a dict of translations for a given field of a model instance.
:param obj: The model instance
:param field_name: The name of the field
:return: A dict with language codes as keys and translations as values
"""

return {
lang_code: getattr(obj.translations.filter(language_code=lang_code).first(), field_name, "")
for lang_code, _ in settings.LANGUAGES
}

0 comments on commit dd1b721

Please sign in to comment.