【问题标题】:How to change Site model object in django data migration?如何在 django 数据迁移中更改站点模型对象?
【发布时间】:2020-01-22 04:27:35
【问题描述】:

我想要做的是将站点显示名称和域从 example.com 更改为 mydomain.com 。通常,我可以进入 django 管理员并执行此操作。但我想使用数据迁移。我的代码如下:

from django.db import migrations


def change_site_name_from_example_to_mydomain_func(apps, schema_editor):
    Site = apps.get_model('sites', 'Site')
    site = Site.objects.get(name='example.com')
    site.name = 'mydomain.com'
    site.domain = 'mydomain.com'
    site.save()


class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0006_populate_database_createsuperuser'),
    ]

    operations = [
        migrations.RunPython(change_site_name_from_example_to_mydomain_func),
    ]

但是,我收到一条错误消息,提示没有网站这样的应用。问题是,如何在数据迁移中使用站点模型? 错误是这样的: LookupError: No installed app with label 'sites'.

【问题讨论】:

  • Site是在什么应用中定义的,乍一看好像是在'accounts'中定义的,而不是'sites'
  • 这个应用是django预定义的
  • 可以通过from django.contrib.sites.models import Site导入
  • 您确定django.contrib.sitesINSTALLED_APPS 的列表中吗?
  • 但是,在数据迁移中,我应该得到一个apps.get_app('app', 'model_name)的应用程序,我不知道如何为Site做它

标签: python django django-migrations


【解决方案1】:

您链接到的问题建议您向站点应用程序添加依赖项,例如

dependencies = [
    ('accounts', '0006_populate_database_createsuperuser'),
    ('sites', '0002_alter_domain_unique'),
]

【讨论】:

  • 我遇到了同样的错误:LookupError: No installed app with label 'django.contrib.sites'.
  • 确保您的原始问题中有Site = apps.get_model('sites', 'Site')。如果它仍然不起作用,请使用新代码和完整的回溯更新您的问题。
  • 你能告诉我在哪里可以看到这个迁移文件吗??!! 0002_alter_domain_unique@Alasdair
  • 它在django.contrib.sites 应用程序中。该迁移的内容无关紧要 - 我们使用它是因为它是站点应用程序的最后一次迁移(在当前版本的 Django 中)。
  • 顺便说一句,它并没有解决问题。好像是django的问题,基于ticket
【解决方案2】:

你试过python manage.py migrate sites吗? 然后再试一次python manage.py migrate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2022-06-17
    • 2014-05-01
    • 1970-01-01
    • 2021-06-02
    • 2014-07-01
    • 2020-07-21
    相关资源
    最近更新 更多