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 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 . - 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 change django_content_type.model entry in DB. For PostgreSQL use
UPDATE django_content_type SET model='<newModelName>'
where model='<oldModelName>' AND app_label='<OldAppName>'
- if you're using the new migrations, you'll need to change the app name in the existing migrations files (such as dependency module names) and
django_migrations
table. It might be better to squash migrations first so there's less to edit. - For Postgres, If you want to rename the sequence too, then use
ALTER SEQUENCE <oldAppName>_<modelName>_<PK>_seq
RENAME TO <newAppName>_<modelName>_<PK>_seq
Although it is not necessary, the system itself doesn't care about the name. The column DEFAULT stores an OID ('foo_pkey_seq'::regclass), you can change the name of the sequence without breaking that - the OID stays the same.
网友评论