Django模型翻译太多领域

时间:2018-03-19 14:54:16

标签: python django translation admin django-modeltranslation

我想使用django-modeltranslation翻译django应用程序(1.11)中的name字段。我想翻译为en和fr,但在管理面板中,我得到3个字段而不是2个:namename_enname_fr

models.py

class Country(models.Model):
    name = models.CharField(max_length=100)
    code = models.SlugField(max_length=20, default='')

    def __str__(self):
        return self.name

admin.py

class CountryAdmin(admin.ModelAdmin):
    list_display = ('name_en',)

translation.py

from events.models import Country

class CountryTranslationOptions(TranslationOptions):
    fields = ('name',)
translator.register(Country, CountryTranslationOptions)

1 个答案:

答案 0 :(得分:2)

请按照

从TranslationAdmin(而不是admin.ModelAdmin)继承您的管理模型

http://django-modeltranslation.readthedocs.io/en/latest/admin.html

F。你应该

from modeltranslation.admin import TranslationAdmin

class CountryAdmin(TranslationAdmin):
    list_display = ('name',)