Skip to main content

Translations for Django models

Project description

Adding translations to Django models is a topic that has been discussed from a vast variety of angles, and yet still not very well defined. django-i18n-model is yet another solution for adding i18n to your models.

It is very similar to django-hvad in that it uses the actual database and metaclasses to do its job, but unlike django-hvad, it does not modify the source model. Unlike django-modeltranslation, and like django-hvad, django-i18n-models does not add any new fields to the source model.

One interesting library for handling i18n in models is django is django-lingua. Unlike any of the database-backed solutions, it uses the gettext interface to facilitate translation of model data. While we find lingua interesting in principle, we believe translation of database data should be kept in the database (and lingua didn’t work very well for us anyway).

The main advantage of using django-i18n-model is the ability to:

  1. Add custom fields to translations
  2. Ability to use South migrations
  3. Not necessary to modify your existing models

Keep in mind that this library is fairly young so it still lacks many of the convenience features such as automatic translation of fields and admin integration (features that other solutions do promise). Those features are still being designed and are planned for future releases.


django-i18n-model works by creating a completely separate model for translation. It does so by obtaining information about the model fields from the source model and creating a clone with additional fields called i18n_language and i18n_source. It currently offers several ways of referencing the translation source model and the set of fields to include in the translations.


Install using pip or easy_install:

pip install django-i18n-model

easy_install django-i18n-model

You can also download the tarball and unpack it into your project directory.

If you want to use the supplied template tags, you also need to add the i18N_model app to INSTALLED_APPS.

Basic usage

To create a new translation model, simply subclass the I18nModel class:

from django.db import models
from i18n_model.models import I18nModel

class Source(models.Model):
    """ Your normal model """
    title = models.CharField(max_length=20)
    body = models.TextField()
    date = models.DateField()

class SourceI18N(I18nModel):
    class Meta:
        translation_fields = ('title', 'body')

With the above setup, a new model is created that is named SourceI18N and it will contain the title, body, i18n_language and i18n_source fields. The i18n_source is a foreign key to Source model.

Other than adding the ‘I18N’ suffix to the translation model name, you can also use the source_model Meta option to reference the source model. For example:

class SourceTranslation(I18nModel):
    class Meta:
        source_model = Source
        translation_fields = ('title', 'body')

The source_model attribute can point to the class object directly, or it can use a string name of the class (ex: 'Source') or, if the model is in a different app, you can also use the 'app.Model' format commonly used in Django. The following are all equivalent:

class SourceTranslation(I18nModel):
    class Meta:
        source_model = Source

class SourceTranslation(I18nModel):
    class Meta:
        source_model = 'Source'

class SourceTranslation(I18nModel):
    class Meta:
        source_model = 'appname.Source'

Creating translations

You can create translations as usual by simply creating a new instance of the *I18N model, or you can use the translate class method on the *I18N class. Here is an example of the latter using the above code:

my_source = Source(title='Test', body='test',
my_translation = SourceI18N.translate(

Getting translations

The translations are obtained using the translate class method. You can obtain translations for a specific language by calling the translate class method without any keyword arguments:

translation = SourceI18N.translate(my_source, 'sr')
translation.title  # >> 'Тест'
translation.body  # >> тест'

It is also possible to obtain translations directly from the source model. The foreign key on the translation model creates a translations property on the source model. This property is an instance of I18nManager custom manager, and it behaves like a normal Django manager for most part. To get all translations for a given object:


To get translations for a specific language, the manager has shortcut manager methods that are named after locales:

translation =

Retrieving translations programmatically

Although the hard-coded locale methods are useful in templates, you may sometimes need to retrieve translations with variable locale. In that case, you may want to use the lang manager method instead. Here’s an example:




Using the lang method without any language code will filter languages for the currently active language:

my_source.translations.lang().get()  # Gets translation for 'de' language

The current_language manager method is a deprecated alias for the last form.

Template tags

To use the template tags first load the i18n_model library:

{% load i18n_model %}

{% translate %} tag

Translate tag is an assignment tag. It takes the source object, and returns a translation object that you can use in your template. For example:

{% translate my_source as my_translation %}
{{ my_translation.title }}
{{ my_translation.body }}

By default, it uses the currently active language for looking up translation. It will return the original source object if there is no matching translation.

Note that non-translated fields from the original model are not copied to the translation. For non-translated fields, always use the original.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-i18n-model, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size django-i18n-model-0.0.4.tar.gz (7.3 kB) File type Source Python version None Upload date Hashes View
Filename, size (9.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page