Translating Fields on Models

The translations app defines a Translation model, but for the most part, you shouldn’t have to use that directly. When you want to create a foreign key to the translations table, use translations.fields.TranslatedField. This subclasses Django’s django.db.models.ForeignKey to make it work with our special handling of translation rows.

A minimal Addon model looks like this:

import amo.models
from translations.fields import TranslatedField

class Addon(amo.models.ModelBase):
    name = TranslatedField()
    description = TranslatedField()

Creating New Translations

If you need to create new Translations without the automagic helpers behind TranslatedField, use Translation.new.

classmethod Translation.new(string, locale, id=None)

Jumps through all the right hoops to create a new translation.

If id is not given a new id will be created using translations_seq. Otherwise, the id will be used to add strings to an existing translation.

translations.fields

class translations.fields.TranslatedField(**kwargs)

A foreign key to the translations table.

If require_locale=False, the fallback join will not use a locale. Instead, we will look for 1) a translation in the current locale and 2) fallback with any translation matching the foreign key.

translations.models

class translations.models.Translation(*args, **kwargs)

Translation model.

Use translations.fields.TranslatedField instead of a plain foreign key to this model.

class translations.models.TranslationSequence(*args, **kwargs)

The translations_seq table, so syncdb will create it during testing.

Table Of Contents

Previous topic

Testing

This Page