How to rename app in Django

Follow these steps to change an app's name in Django:

  1. Rename the folder which is in your project root
  2. Change any references to your app in their dependencies, i.e. the app's views.py, urls.py , 'manage.py', and settings.py files.
  3. Edit the database table django_content_type with the following command:
UPDATE django_content_type SET app_label='' 
WHERE app_label=''
  1. Also if you have models, you will have to rename the model tables. For postgres use
ALTER TABLE _modelName 
RENAME TO _modelName.
  1. (For Django >= 1.7) Update the django_migrations table to avoid having your previous migrations re-run:
UPDATE django_migrations SET app=''
WHERE app=''. 

Note: there is some debate (in comments) if this step is required for Django 1.8+; If someone knows for sure please update here.

  1. If your models.py's Meta Class has app_name listed, make sure to rename that too .
  2. 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.
  3. 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='' 
where model='' AND app_label=''
  1. 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.
  2. For Postgres, If you want to rename the sequence too, then use
ALTER SEQUENCE ___seq 
RENAME TO ___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.

你可能感兴趣的:(How to rename app in Django)