Reference: Models

This module contains the models for the Translations app.

class translations.models.Translation[source]

The model which represents the translations.

Each translation belongs to a unique database address. Each address is composed of a content_type (table), an object_id (row) and a field (column). Each unique address must have only one translation text in a specific language.

Note

content_type and object_id together form something called a GenericForeignKey. This kind of foreign key contrary to the normal foreign key (which can point to a row in only one table) can point to a row in any table.

Note

object_id is defined as a CharField so that it can also point to the rows in the tables which use character fields (like UUIDField, etc.) as primary key.

Warning

Try not to work with the Translation model directly unless you really have to and you know what you are doing.

To create the translation of a field manually:

from django.contrib.contenttypes.models import ContentType
from translations.models import Translation
from sample.models import Continent

europe = Continent.objects.get(code='EU')

translation = Translation.objects.create(
    content_type=ContentType.objects.get_for_model(Continent),
    object_id=europe.pk,
    field='name',
    language='de',
    text='Europa',
)

print(translation)
Europe: Europa
class translations.models.Translatable[source]

An abstract model which provides custom translation functionalities.

Marks the subclasses as translatable and creates some default configurations for them based on their structure.

To make a model translatable:

from translations.models import Translatable
class Continent(Translatable):
    name = models.CharField(
        verbose_name=_('name'),
        help_text=_('the name of the continent'),
        max_length=64,
    )
    denonym = models.CharField(
        verbose_name=_('denonym'),
        help_text=_('the denonym of the continent'),
        max_length=64,
        blank=True,
    )
    code = models.CharField(
        verbose_name=_('code'),
        help_text=_('the code of the continent'),
        max_length=2,
        unique=True,
        primary_key=True,
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = _('continent')
        verbose_name_plural = _('continents')

It also adds the translations relation to the model, just in case any one wants to work with the translations of an instance manually.

Note

The translations relation is the reverse relation of the GenericForeignKey described in Translation. It’s a GenericRelation.

class TranslatableMeta[source]

This class contains meta information about the translation of the model instances.

fields

The names of the fields to use in the translation.

By default it is set to None. This means the translation will use the text based fields automatically. (like CharField and TextField - this does not include EmailField or the fields with choices)

If needed, it can be set to nothing. This can be done by explicitly setting it to [].

To set the translatable fields of a model:

class Continent(Translatable):
    name = models.CharField(
        verbose_name=_('name'),
        help_text=_('the name of the continent'),
        max_length=64,
    )
    denonym = models.CharField(
        verbose_name=_('denonym'),
        help_text=_('the denonym of the continent'),
        max_length=64,
        blank=True,
    )
    code = models.CharField(
        verbose_name=_('code'),
        help_text=_('the code of the continent'),
        max_length=2,
        unique=True,
        primary_key=True,
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = _('continent')
        verbose_name_plural = _('continents')

    class TranslatableMeta:
        fields = ['name', 'denonym']
classmethod get_translatable_fields(cls)[source]

Return the model’s translatable fields.

Returns the model’s translatable fields based on the field names listed in TranslatableMeta.fields.

Returns:

The translatable fields of the model.

Return type:

list(Field)

To get the mentioned model’s translatable fields:

from sample.models import Continent

for field in Continent.get_translatable_fields():
    print(field)
sample.Continent.name
sample.Continent.denonym
classmethod _get_translatable_fields_names(cls)[source]

Return the names of the model’s translatable fields.

Returns the names of the model’s translatable fields based on the field names listed in TranslatableMeta.fields.

Returns:

The names of the model’s translatable fields.

Return type:

list(str)

To get the names of the mentioned model’s translatable fields:

from sample.models import Continent

for name in Continent._get_translatable_fields_names():
    print(name)
name
denonym
classmethod _get_translatable_fields_choices(cls)[source]

Return the choices of the model’s translatable fields.

Returns the choices of the model’s translatable fields based on the field names listed in TranslatableMeta.fields.

Returns:

The choices of the model’s translatable fields.

Return type:

list(tuple(str, str))

To get the choices of the mentioned model’s translatable fields:

from sample.models import Continent

for choice in Continent._get_translatable_fields_choices():
    print(choice)
(None, '---------')
('name', 'Name')
('denonym', 'Denonym')