我在Django中修改了一个应用的名字,重命名了它的文件夹、导入和所有引用(模板/索引)。但是现在,当我试图运行python manage.py runserver时,我得到了这个错误

Error: Could not import settings 'nameofmynewapp.settings' (Is it on sys.path?): No module named settings

如何调试和解决此错误?有线索吗?


当前回答

我的简单替代方案避免了手工数据库操作(类似于https://stackoverflow.com/a/64133141/1608851),在CI/CD中工作得很好,并且是可逆的。

在“旧”应用程序中创建一个空迁移,重命名表、内容类型和迁移记录。包括反向迁移。 例子:

from django.db import migrations

sql = """
ALTER TABLE oldapp_examplemodel RENAME TO newapp_examplemodel;
UPDATE django_migrations SET app ='newapp' WHERE app ='oldapp';
UPDATE django_content_type SET app_label ='newapp' WHERE app_label ='oldapp';
"""

reverse_sql = """
ALTER TABLE newapp_examplemodel RENAME TO oldapp_examplemodel;
UPDATE django_migrations SET app ='oldapp' WHERE app ='newapp';
UPDATE django_content_type SET app_label ='oldapp' WHERE app_label ='newapp';
"""


class Migration(migrations.Migration):

    dependencies = [
        ('oldapp', '0001_initial'),
    ]

    operations = [
        migrations.RunSQL(sql, reverse_sql)
    ]

将模型表名设置为new_app。 例子:

class ExampleModel(models.Model):

    # normal column definitions...

    class Meta:
        db_table = "newapp_examplemodel"  # add this line to the meta class

提交、测试和部署上述更改。 删除步骤1中创建的迁移。 重命名/移动旧应用程序并修复所有引用(参见https://stackoverflow.com/a/8408131/1608851了解更多详细信息),包括迁移中的引用。 从模型元类中删除db_table覆盖。 提交、测试和部署。

注:

The django_migrations table will retain a row with the old app, because that row is added after our new migration was run, but before it was registered under the new app name. It should be okay to leave that as is. Deleting the migration is due to the above point, but also to prevent future issues when reverse migrating, but also because Reversing this app rename requires checking out / deploying the commit from step 3, then reversing our migration, then checking out / deploying the commit before that

其他回答

在很多情况下,我认为@allcaps的答案效果很好。

然而,有时确实需要重命名应用程序,例如提高代码可读性或防止混淆。

大多数其他答案都涉及手工数据库操作或对现有迁移进行修补,我不太喜欢这种方法。

作为一种替代方法,我喜欢用想要的名称创建一个新的应用程序,复制所有内容,确保它可以工作,然后删除原来的应用程序:

Start a new app with the desired name, and copy all code from the original app into that. Make sure you fix the namespaced stuff, in the newly copied code, to match the new app name. makemigrations and migrate. Note: if the app has a lot of foreign keys, there will likely be issues with clashing reverse accessors when trying to run the initial migration for the copied app. You have to rename the related_name accessor (or add new ones) on the old app to resolve the clashes. Create a data migration that copies the relevant data from the original app's tables into the new app's tables, and migrate again.

此时,一切仍然正常,因为原始应用程序及其数据仍然在原地。

现在你可以重构所有的依赖代码,所以它只使用新的应用程序。有关需要注意的例子,请参阅其他答案。 一旦你确定一切正常,你可以删除原来的应用程序。这涉及到另一种(模式)迁移。

这样做的好处是,每一步都使用正常的Django迁移机制,不需要手动操作数据库,我们可以跟踪源代码控制中的所有内容。此外,我们将保留原始应用程序及其数据,直到我们确定一切正常为止。

为什么不直接使用“查找和替换”选项呢?(每个代码编辑器都有)?

例如Visual Studio Code(在Edit选项下):

您只需输入旧名称和新名称,并替换项目中的所有内容,只需单击一下。

注意:这只重命名文件内容,而不是文件和文件夹名称。不要忘记重命名文件夹,例如。Templates /my_app_name/重命名为Templates /my_app_new_name/

简单的4步重命名Django项目中现有的应用程序,没有任何痛苦或数据丢失。

步骤1

重命名app文件夹。在本例中,“old_app”是我们的旧名称,“new_app”是我们的新名称。

Mv ./old_app ./new_app . Mv ./old_app . Mv

步骤2

更新所有引用旧文件夹的导入以引用新文件夹。

例如:

# Before 
from myproject.old_app import models

# After
from myproject.new_app import models

步骤3

更新Django迁移中旧的应用程序名称引用。

你可能需要做的一些改变:

# Before

dependencies = [
    ('old_app', '0023_auto_20200403_1050'),
]

# After

dependencies = [
    ('new_app', '0023_auto_20200403_1050'),
]

# Before

field = models.ForeignKey(
    default=None, on_delete=django.db.models.deletion.CASCADE,
    to='old_app.Experiment'
)

# After

field = models.ForeignKey(
    default=None, on_delete=django.db.models.deletion.CASCADE,
    to='new_app.Experiment'
)

步骤4

在这一点上做出承诺。

然后,无论您在已部署的环境中运行应用程序迁移,都要在该进程中运行迁移之前运行django_rename_app。

即在"python manage.py migrate——noinput"之前,如下面的例子所示。

# Before

python manage.py collectstatic --noinput
python manage.py migrate --noinput
gunicorn my_project.wsgi:application

# After

python manage.py collectstatic --noinput
python manage.py rename_app old_app new_app
python manage.py migrate --noinput
gunicorn my_project.wsgi:application

这将更新Django内部数据库表中的应用程序名称:

django_content_type django_migrations

并重命名所有表的前缀,以新的应用程序名称开始,而不是旧的。

就是这样。

注意:最好使用IDE /文本编辑器的查找和替换功能来更改各种文件。

Django 1.7新增了一个应用注册表,用于存储配置并提供自省功能。这个机制让你改变几个应用程序的属性。

我想说的主要观点是,重命名应用并不总是必要的:通过应用配置,可以解决冲突的应用。但如果你的应用需要友好的命名,这也是一种方法。

例如,我想把我的民意调查应用程序命名为“用户反馈”。它是这样的:

在polls目录下创建一个apps.py文件:

from django.apps import AppConfig

class PollsConfig(AppConfig):
    name = 'polls'
    verbose_name = "Feedback from users"

在polls/__init__.py中添加默认应用配置:

default_app_config = 'polls.apps.PollsConfig'

更多应用配置请访问:https://docs.djangoproject.com/en/1.7/ref/applications/

如果你使用Pycharm,重命名应用程序是非常容易的重构(Shift+F6默认)的所有项目文件。 但是要确保你删除了项目目录及其子目录中的__pycache__文件夹。也要小心,因为它也重命名注释,你可以排除在重构预览窗口,它会显示给你。 此外,你还必须在重命名应用的apps.py中重命名OldNameConfig(AppConfig):。

如果你不想丢失数据库中的数据,你必须手动在数据库中查询,就像前面提到的答案。