django-modeltranslation成为可翻译

时间:2017-11-09 18:34:29

标签: django translation

我正在使用 django-modeltranslation 在我的应用中翻译模型。我有一个模型,我使用migrate命令与db同步。我也有很多关于该模型的记录。

class Home(models.Model):
    description = models.TextField()

当然,此时,我可以从db

中检索描述字段
h = Home.objects.first()
h.description # --> "This home is near to..."

现在,我希望使用django-modeltranslation成为可翻译的 description 字段。我跟着de guide,我已经在 translation.py 文件中注册了要翻译的模型,最后我执行了 makemigrations migrate 命令。这添加到我的数据库,在主页表中,字段 description_en description_es ,因为我的可用语言是 en es ,前者是默认值。

此时我需要填充description_en字段,这是任何查询的默认值,我试过

Home.objects.all().update(description_en=F('description')) 

但它不起作用,因为当它试图访问 description 字段时,它实际上是在尝试访问 description_en ,它是空的:

h = Home.objects.first()
h.description # --> '' Empty?!!!

我检查数据是否仍然在数据库中,并且它们是!

我的问题是:如果 description 数据仍然在db中, h.description 实际上检索到我 h.description_en < / em>,如何为 description 字段中的所有数据填充 description_en

1 个答案:

答案 0 :(得分:0)

阅读这个django-modelstranslation fallback original field value,我意识到存在与modeltranslation一起提供的管理命令,并填充默认的lang: python manage.py update_translation_fields