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
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表以避免重新运行以前的迁移:UPDATE django_migrations SET app='<NewAppName>' WHERE app='<OldAppName>'。注意:对于Django 1.8+是否需要执行此步骤,存在一些参数(在注释中)。如果有人知道,请在这里更新。
要重命名django models,您需要更改django_content_type.nameDB中的条目。对于PostgreSQL使用UPDATE django_content_type SET name='<newModelName>' where name='<oldModelName>' AND app_label='<OldAppName>'
元点(如果使用virtualenv):值得注意的是,如果要重命名包含virtualenv的目录,则env中可能有几个文件包含绝对路径,并且也需要更新。如果您遇到诸如此类的错误,ImportError: No module named ...可能是罪魁祸首。(感谢@danyamachine提供此功能)。
Follow these steps to change an app’s name in Django:
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’ , and settings.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 has app_name listed, make sure to rename that too (mentioned by @will).
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.
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 (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
New in Django 1.7 is a app registry that stores configuration and provides introspection. This machinery let’s you change several app attributes.
The main point I want to make is that renaming an app isn’t always necessary: With app configuration it is possible to resolve conflicting apps. But also the way to go if your app needs friendly naming.
As an example I want to name my polls app ‘Feedback from users’. It goes like this:
Create a apps.py file in the polls directory:
from django.apps import AppConfig
class PollsConfig(AppConfig):
name = 'polls'
verbose_name = "Feedback from users"
Add the default app config to your polls/__init__.py:
This can painlessly be done IF other apps do not foreign key models from the app to be renamed. Check and make sure their migration files don’t list any migrations from this one.
Backup your database. Dump all tables with a) data + schema for possible circular dependencies, and b) just data for reloading.
Run your tests.
Check all code into VCS.
Delete the database tables of the app to be renamed.
Delete the permissions: delete from auth_permission where content_type_id in (select id from django_content_type where app_label = '<OldAppName>')
Delete content types: delete from django_content_type where app_label = '<OldAppName>'
Rename the folder of the app.
Change any references to your app in their dependencies, i.e. the app’s views.py, urls.py , ‘manage.py’ , and settings.py files.
Delete migrations: delete from django_migrations where app = '<OldAppName>'
If your models.py ‘s Meta Class has app_name listed, make sure to rename that too (mentioned by @will).
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.
If you defined app config in apps.py; rename those, and rename their references in settings.INSTALLED_APPS
If you use Pycharm, renaming an app is very easy with refactoring(Shift+F6 default) for all project files.
But make sure you delete the __pycache__ folders in the project directory & its sub-directories. Also be careful as it also renames comments too which you can exclude in the refactor preview window it will show you.
And you’ll have to rename OldNameConfig(AppConfig): in apps.py of your renamed app in addition.
If you do not want to lose data of your database, you’ll have to manually do it with query in database like the aforementioned answer.
Why not just use the option Find and Replace. (every code editor has it)?
For example Visual Studio Code (under Edit option):
You just type in old name and new name and replace everyhting in the project with one click.
NOTE: This renames only file content, NOT file and folder names. Do not forget renaming folders, eg. templates/my_app_name/ rename it to templates/my_app_new_name/