微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将 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 举报,一经查实,本站将立刻删除。