How to change the name of a Django app?
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
, 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>'
- Update 16Jul2021: Also, the
__pycache__/
folder inside the app must be removed, otherwise you getEOFError: marshal data too short when trying to run the server
. Mentioned by @Serhii Kushchenko
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!)
Easily rename Django project
Renaming the project is actually easier than renaming an app. This question explains how to rename an app.
To rename the project, you need to change the project name wherever it appears. grep -nir oldname .
can help you find where it appears. In my case, I had to change the following places:
Rename the
oldprojectname
directory tonewprojectname
manage.py
: Changeos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oldprojectname.settings')
newprojectname/wsgi.py
: Changeos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oldprojectname.settings')
newprojectname/settings.py
: ChangeROOT_URLCONF = 'oldprojectname.urls'
and changeWSGI_APPLICATION = 'oldprojectname.wsgi.application'
newprojectname/urls.py
: Change oldprojectname in a line I had added
How to rename a Django app and migrate data from an app to the other
I found a solution that's works
- Fix old migrations with new Foreign Keys and new app dependencies.
- Force old migrations to create tables with old app name, so for that in
migrations.CreateModel.options
, adddb_table: 'app1_table_name'
- In each migration file add
replaces = [('app1', 'migration_file_name')]
. This will tell to Django that current migration (app2.migration_file_name
) will replace the old file, this will prevenent django to execute migrations twice. - Create a migration file to rename tables with
migrations.AlterModelTable
Can you give a Django app a verbose name for use throughout the admin?
Django 1.8+
Per the 1.8 docs (and current docs),
New applications should avoid
default_app_config
. Instead they should require the dotted path to the appropriateAppConfig
subclass to be configured explicitly inINSTALLED_APPS
.
Example:
INSTALLED_APPS = [
# ...snip...
'yourapp.apps.YourAppConfig',
]
Then alter your AppConfig
as listed below.
Django 1.7
As stated by rhunwicks' comment to OP, this is now possible out of the box since Django 1.7
Taken from the docs:
# in yourapp/apps.py
from django.apps import AppConfig
class YourAppConfig(AppConfig):
name = 'yourapp'
verbose_name = 'Fancy Title'
then set the default_app_config
variable to YourAppConfig
# in yourapp/__init__.py
default_app_config = 'yourapp.apps.YourAppConfig'
Prior to Django 1.7
You can give your application a custom name by defining app_label in your model definition. But as django builds the admin page it will hash models by their app_label, so if you want them to appear in one application, you have to define this name in all models of your application.
class MyModel(models.Model):
pass
class Meta:
app_label = 'My APP name'
Related Topics
Python Parse CSV Ignoring Comma with Double-Quotes
Pandas Extract Number from String
Python How to Parse CSS File as Key Value
Why Does a Class' Body Get Executed at Definition Time
List Running Processes on 64-Bit Windows
How to Use Selenium to Automate Chase Site Login
Pandas New Column from Groupby Averages
Pandas: Replace Substring in String
Understanding Recursion in Python
Is This Bad Programming Practice in Tkinter
How to Rotate the Sprite and Shoot the Bullets Towards the Mouse Position
Color Coding Cells in a Table Based on the Cell Value Using Jinja Templates
How to Pull Out CSS Attributes from Inline Styles with Beautifulsoup
How to Decrypt Aws Ruby Client-Side Encryption in Python
Extract Column Value Based on Another Column Pandas Dataframe