如何更改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:
- 重命名项目根目录中的文件夹
- 在依赖关系中更改对应用程序的任何引用,即应用程序的
views.py
,urls.py
,'manage.py'和settings.py
文件。 - 使用以下命令编辑数据库表
django_content_type
:UPDATE django_content_type SET app_label ='< ; NewAppName>'WHERE app_label ='< OldAppName>'
- 此外,如果您有模型,则必须重命名模型表。对于postgres,使用
ALTER TABLE< oldAppName> _modelName RENAME TO< newAppName> _modelName
。对于mysql,我认为它是一样的(如@null_radix所提到的) - (对于Django> = 1.7)更新
django_migrations
表,以避免以前的迁移重新运行:更新django_migrations SET app ='< NewAppName>'WHERE app ='< OldAppName>'
。 注意:如果Django 1.8+需要此步骤,则有一些争议(在评论中)如果有人知道,请在这里更新。 - 如果您的
models.py
的Meta类具有app_name
列出,请确保重命名(@will提到)。 - 如果您命名为$
static
或模板
您的应用程序中的文件夹,您还需要重命名这些文件夹。例如,将old_app / static / old_app
重命名为new_app / static / new_app
。 - 要重命名django
模型
,您需要更改DB中的django_content_type.name
条目。对于postgreSQL,使用UPDATE django_content_type SET name ='< newModelName>'其中name ='< oldModelName>'AND app_label ='< OldAppName>'
$值得注意的是,如果您正在重命名包含您的virtualenv的目录,那么可能会出现以下情况:b $ b
- Rename the folder which is in your project root
- Change any references to your app in their dependencies, i.e. the app's
views.py
,urls.py
, 'manage.py' , andsettings.py
files. - Edit the database table
django_content_type
with the following command:UPDATE django_content_type SET app_label='<NewAppName>' WHERE app_label='<OldAppName>'
- 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) - (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. - If your
models.py
's Meta Class hasapp_name
listed, make sure to rename that too (mentioned by @will). - If you've namespaced your
static
ortemplates
folders inside your app, you'll also need to rename those. For example, renameold_app/static/old_app
tonew_app/static/new_app
. - For renaming django
models
, you'll need to changedjango_content_type.name
entry in DB. For postgreSQL useUPDATE 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
- 使用Django和South重命名应用
- 如何将模型从一个django应用程序迁移到一个新的应用程序?
- 如何更改Django应用程序的名称?
- 使用Django South向后移动
- 最简单的方法使用Django / South重命名模型?
- Renaming an app with Django and South
- How do I migrate a model out of one django app and into a new one?
- How to change the name of a Django app?
- Backwards migration with Django South
- Easiest way to rename a model using Django/South?