API Reference

TranslatedVirtualField

class modeltrans.fields.TranslatedVirtualField(original_field, language=None, *args, **kwargs)[source]

A field representing a single field translated to a specific language.

Parameters:
  • original_field – The original field to be translated
  • language – The lanuage to translate to, or None to track the current active Django language.
get_field_name()[source]

Returns the field name for the current virtual field.

The field name is <original_field_name>_<language> in case of a specific translation or <original_field_name>_i18n for the currently active language.

get_language()[source]

Returns the language for this field.

In case of an explicit language (title_en), it returns ‘en’, in case of title_i18n, it returns the currently active Django language.

TranslationField

class modeltrans.fields.TranslationField(fields=None, required_languages=None, virtual_fields=True, *args, **kwargs)[source]

This model field is used to store the translations in the translated model.

Parameters:
  • fields (iterable) – List of column names to make translatable.
  • required_languages (iterable) – List of languages required for the model.
  • virtual_fields (bool) – If False, do not add virtual fields to access translated values with. Set to True during migration from django-modeltranslation to prevent collisions with it’s database fields while having the i18n field available.

MultiLingualManager

class modeltrans.manager.MultilingualManager[source]

When adding the modeltrans.fields.TranslationField to a model, MultilingualManager is automatically mixed in to the manager class of that model.

MultilingualQuerySet

class modeltrans.manager.MultilingualQuerySet(model=None, query=None, using=None, hints=None)[source]

Extends Queryset and makes the translated versions of fields accessible through the normal queryset methods, analogous to the virtual fields added to a translated model:

  • <field> allow getting/setting the default language
  • <field>_<lang> (for example, <field>_de) allows getting/setting a specific language. Note that if DEFAULT_LANGUAGE == 'en', <field>_en is mapped to <field>.
  • <field>_i18n follows the currently active translation in Django, and falls back to the default language.

When adding the modeltrans.fields.TranslationField to a model, MultilingualManager is automatically mixed in to the manager class of that model.

add_i18n_annotation(field, annotation_name=None, fallback=True)[source]

Private method to add an annotation to the query to extract the translated version of a field from the jsonb field to allow filtering and ordering.

Parameters:
  • field (TranslatedVirtualField) – the virtual field to create an annotation for.
  • annotation_name (str) – name of the annotation, if None (by default), <original_field>_<lang>_annotation will be used.
  • fallback (bool) – If True, COALESCE will be used to get the value of the original field if the requested translation is not in the i18n dict.
Returns:

the name of the annotation.