March 2020
Intermediate to advanced
608 pages
17h 17m
English
Execute the following steps to prepare for multilingual models:
# myproject/apps/core/model_fields.pyfrom django.conf import settingsfrom django.utils.translation import get_languagefrom django.utils import translationclass TranslatedField(object): def __init__(self, field_name): self.field_name = field_name def __get__(self, instance, owner): lang_code = translation.get_language() if lang_code == settings.LANGUAGE_CODE: # The fields of the default language are in the main model return getattr(instance, self.field_name) else: # The fields of the other languages are in the translation # model, but falls back to the main model translations = instance.translations.filter( ...