How to change the name of a Django app?
django, python
Solution
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>'`
- Update 16Jul2021: Also, the `__pycache__/` folder inside the app must be removed, otherwise you get `EOFError: marshal data too short when trying to run the server`. Mentioned by @Serhii Kushchenko
- Update the apps.py file in the app you're renaming to use the new name. Change the "<old_app_name>Config" class name to "<new_app_name>Config" and within this class change name = '<old_app_name>' to name = '<new_app_name>'
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 to the below links for a more complete picture:
- 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?
- Python code (thanks to A.Raouf) to automate the above steps (Untested code. You have been warned!)
- Python code (thanks to rafaponieman) to automate the above steps (Untested code. You have been warned!)
Problem
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?