将 django-modeltranslation 添加到现有项目会破坏测试

问题描述

我有现有项目,我决定添加翻译选项。我安装了 django-modeltranslation,在设置中添加了语言,在 translation.py添加了模型,并在 admin.py添加了模型。

from modeltranslation.translator import translator,Translationoptions
from .models import GaMetask


class GaMetaskTranslationoptions(Translationoptions):
    fields =('name','description','button_text')


translator.register(GaMetask,GaMetaskTranslationoptions)
@admin.register(GaMetask)
class GaMetaskAdmin(TranslationAdmin):
    model = GaMetask

但正如我所提到的,我将其添加到现有项目中。所以我必须将现有数据移动到创建的字段。所以在 makemigrations 之后但在 migrate 之前,我添加到我的迁移文件中:

def populate_function(apps,schema_editor):
    management.call_command("update_translation_fields")

class Migration(migrations.Migration):

    dependencies = [
        ('pages','0054_auto_20210105_1041'),]

    operations = [
        .........,migrations.RunPython(populate_function,migrations.RunPython.noop),]

之后我运行 python manage.py migrate pages migrate_file。它有效。我更新了我的数据库并且我的数据没有丢失,它们被移动到设置中设置的认语言字段。但在运行我的测试后,我得到:

django.db.utils.ProgrammingError: column pages_gaMetask.button_text_pl does not exist. 
LINE 1: ..._text_pl" = "pages_gaMetask"."button_text" WHERE ("pages_gam...
                                                             ^
HINT:  Perhaps you meant to reference the column "pages_gaMetask.button_text".

总而言之,我有我想要的 - 我的数据库中有我的语言的字段,数据没有丢失,但是在测试过程中,由于语言的字段,数据库无法创建。有人可以帮助寻找解决方案吗?如果需要更多信息,请告诉我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)