如何更改Django应用程序的名称?

问题描述:

我已经通过重命名其文件夹,导入和所有引用(templates / indexes)来更改Django中的应用程序的名称。但是,当我尝试运行 python manage.py runserver

I have changed the name of an app in Django by renaming its folder, imports and all its references (templates/indexes). But now I get this error when I try to run python manage.py runserver

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

如何调试和解决这个错误?任何线索?

How can I debug and solve this error? Any clues?

按照以下步骤在Django中更改应用程序的名称:

Follow these steps to change an app's name in Django:


  1. 重命名项目根目录中的文件夹

  2. 在依赖关系中更改对应用程序的任何引用,即应用程序的 views.py urls.py ,'manage.py'和 settings.py 文件。

  3. 使用以下命令编辑数据库表 django_content_type UPDATE django_content_type SET app_label ='&lt ; NewAppName>'WHERE app_label ='< OldAppName>'

  4. 此外,如果您有模型,则必须重命名模型表。对于postgres,使用 ALTER TABLE< oldAppName> _modelName RENAME TO< newAppName> _modelName 。对于mysql,我认为它是一样的(如@null_radix所提到的)

  5. (对于Django> = 1.7)更新 django_migrations 表,以避免以前的迁移重新运行:更新django_migrations SET app ='< NewAppName>'WHERE app ='< OldAppName>'注意:如果Django 1.8+需要此步骤,则有一些争议(在评论中)如果有人知道,请在这里更新。

  6. 如果您的 models.py 的Meta类具有 app_name 列出,请确保重命名(@will提到)。

  7. 如果您命名为$ static 模板您的应用程序中的文件夹,您还需要重命名这些文件夹。例如,将 old_app / static / old_app 重命名为 new_app / static / new_app

  8. 要重命名django 模型,您需要更改DB中的 django_content_type.name 条目。对于postgreSQL,使用 UPDATE django_content_type SET name ='< newModelName>'其中name ='< oldModelName>'AND app_label ='< OldAppName>'
  9. $值得注意的是,如果您正在重命名包含您的virtualenv的目录,那么可能会出现以下情况:b $ b
  1. Rename the folder which is in your project root
  2. Change any references to your app in their dependencies, i.e. the app's views.py, urls.py , 'manage.py' , and settings.py files.
  3. Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='<NewAppName>' WHERE app_label='<OldAppName>'
  4. Also if you have models, you will have to rename the model tables. For postgres use ALTER TABLE <oldAppName>_modelName RENAME TO <newAppName>_modelName. For mysql too I think it is the same (as mentioned by @null_radix)
  5. (For Django >= 1.7) Update the django_migrations table to avoid having your previous migrations re-run: UPDATE django_migrations SET app='<NewAppName>' WHERE app='<OldAppName>'. Note: there is some debate (in comments) if this step is required for Django 1.8+; If someone knows for sure please update here.
  6. If your models.py 's Meta Class has app_name listed, make sure to rename that too (mentioned by @will).
  7. If you've namespaced your static or templates folders inside your app, you'll also need to rename those. For example, rename old_app/static/old_app to new_app/static/new_app.
  8. For renaming django models, you'll need to change django_content_type.name entry in DB. For postgreSQL use UPDATE django_content_type SET name='<newModelName>' where name='<oldModelName>' AND app_label='<OldAppName>'

Meta point(如果使用virtualenv)在您的env中包含几个包含绝对路径的文件,并且还需要更新。如果您收到错误,例如 ImportError:没有模块命名为... 这可能是罪魁祸首。 (感谢@danyamachine提供这个)。

Meta point (If using virtualenv): Worth noting, if you are renaming the directory that contains your virtualenv, there will likely be several files in your env that contain an absolute path and will also need to be updated. If you are getting errors such as ImportError: No module named ... this might be the culprit. (thanks to @danyamachine for providing this).

其他参考:您可能还需要参考以下链接以获取更完整的图片

Other references: you might also want to refer the below links for a more complete picture


  1. 使用Django和South重命名应用

  2. 如何将模型从一个django应用程序迁移到一个新的应用程序?

  3. 如何更改Django应用程序的名称?

  4. 使用Django South向后移动

  5. 最简单的方法使用Django / South重命名模型?

  1. Renaming an app with Django and South
  2. How do I migrate a model out of one django app and into a new one?
  3. How to change the name of a Django app?
  4. Backwards migration with Django South
  5. Easiest way to rename a model using Django/South?