'Django ModelTranslation

Hello guys i need a help for translating models values. I use django-modeltranslation package. I have managed to register my models.

class Institution(models.Model):
    title = models.CharField(_('Title'),max_length = 200)
    address = models.CharField(_('Address'),max_length=50)
    pobox = models.CharField(_('Pobox'),max_length=5)
    city = models.CharField(_('City'),max_length=64)
    country = models.CharField(_('Country'),max_length=50)
    telephone = models.CharField(_('Telephone'),max_length = 10)
    def __str__(self):
        return self.title
class Department(models.Model):
    title = models.CharField(_('Title'),max_length = 200)
    address = models.CharField(_('Address'),max_length=50)
    pobox = models.CharField(_('Pobox'),max_length=5)
    city = models.CharField(_('City'),max_length=64)
    country = models.CharField(_('Country'),max_length=50)
    telephone = models.CharField(_('Telephone'),max_length = 10)
    institution=models.ForeignKey(Institution,on_delete=models.CASCADE,verbose_name=_('User'))
    def __str__(self):
        return self.title

Register

@register(Institution)
class InstitutionTranslationOptions(TranslationOptions):
    fields = ('title', 'address','city','country')

@register(Department)
class DepartmentTranslationOptions(TranslationOptions):
    fields = ('title', 'address','city','country')

Here is how i change language:

<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
  <input name="next" type="hidden" value="{{ redirect_to }}">

    <select name="language">
        {% get_current_language as LANGUAGE_CODE %}
        {% get_available_languages as LANGUAGES %}
        {% get_language_info_list for LANGUAGES as languages %}
        {% for language in languages %}
            <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
    <input type="submit" value="Go">
</form>

Ok i open admin i put values for the 2 languages but in template i cant get them... I get only the default value... I use: {{ department.title }} Am I doing something wrong?



Solution 1:[1]

Be sure you did two things:

  1. ran python manage.py sync_translation_fields to sync existing db after adding a new language to your settings.LANGUAGES or a new field to the TranslationOptions of a registered model. Then you need to copy empty fields by your default language by adding the command python manage.py update_translation_fields

  2. add Modeltyranslation to the Admin page admin.py

from modeltranslation.admin import TranslationAdmin
from .models import Institution, Department

class PostAdmin(PostAdmin, TranslationAdmin):
    pass

admin.site.register(Institution, PostAdmin)
admin.site.register(Department, PostAdmin)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vlad Stenkin