Skip to content

Auto-translate on save

When AUTO_TRANSLATE is enabled, django-traduire connects a post_save signal to all models registered with django-modeltranslation.

Enable

TRADUIRE = {
    ...
    "AUTO_TRANSLATE": True,
}

How it works

  1. A model instance is saved
  2. The post_save signal fires
  3. django-traduire checks for empty translation columns
  4. They are filled through the configured backend
  5. The instance is saved again with update_fields (only the translated columns)

The signal handler skips saves that carry update_fields, which is how the second save does not trigger a third. A provider failure is logged and swallowed: a translation API being down must never break a save.

The connection is made once, from the app config, when the setting is True.

Selective auto-translate

Being connected is the real switch. To auto-translate only some models, keep AUTO_TRANSLATE = False — so nothing is connected at start-up — and connect the ones you want:

# In your app's apps.py or ready() method
from django_traduire.signals import connect_model
from myapp.models import Article

connect_model(Article)

Performance note

Auto-translate makes an API call on every save, inside the request that saved the object. On a long article translated into three languages, that is several seconds the user spends watching a spinner — and rich text makes it several requests, not one.

A signal is also invisible branching: nothing in save() tells the next reader that a provider is about to be called.

For anything beyond a small site, call the translation explicitly from the code that decided the write, or from a background task:

from django_traduire import translate_instance


@shared_task
def translate_article(pk):
    translate_instance(Article.objects.get(pk=pk))

For bulk imports, use the management command:

python manage.py traduire myapp.Article