我正在为多语言站点(英语(默认)、阿拉伯语、法语)使用 Django 模型翻译,问题是在 django admin 默认中保存数据时会覆盖阿拉伯语和法语翻译字段。但在数据库中实际值呈现。
我的模型:plan/models.py
class operators(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE)
operator = models.CharField(max_length=100, null=True, blank=True)
image = models.ImageField(verbose_name=_('Operator Logo'), max_length=255,null=True,blank=True)
class Meta:
verbose_name = 'Operator'
verbose_name_plural = 'Operators'
db_table = 'operator'
def __str__(self):
return self.operator
计划/翻译.py:
@register(operators)
class OperatorsTranslationOptions(TranslationOptions):
fields = ('operator',)
计划/admin.py:
class OperatorAdmin(admin.ModelAdmin):
list_display = ('operator', 'country')
.
.
.
admin.site.register(operators, OperatorAdmin)
设置.py:
INSTALLED_APPS = [
'modeltranslation',
.
.
.
'plan',
]
.
.
.
LANGUAGES = (
('en', gettext('English')),
('ar', gettext('Arabic')),
('fr', gettext('French')),
)
.
.
MODELTRANSLATION_TRANSLATION_FILES = (
'plan.translation',
)
MODELTRANSLATION_DEBUG = True
下面是数据库值
planbaker=# select * from operator;
id | operator | image | country_id | operator_ar | operator_en | operator_fr
----+------------+-----------------------+------------+--------------+-------------+-------------
1 | airtel-ind | pb_Zu2y9BE.jpeg | 1 | | |
3 | aircel | 360_360_1_vsXSDEo.JPG | 2 | ايرتل | aircel |
2 | aircel-sa | 352_hQ4TZVq.jpeg | 2 | زين السعودية | aircel-sa | aircel-sa
但是在 Admin 中,所有字段都只有 English 的 operator 值。有人可以告诉我我哪里出错了吗?我尝试了 updte_translation_fields 和 sync_translation_fields 但没有帮助