Skip to content

Instantly share code, notes, and snippets.

@rafaponieman
Created December 6, 2017 00:21
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rafaponieman/201054ddf725cda1e60be3fe845850a5 to your computer and use it in GitHub Desktop.
Save rafaponieman/201054ddf725cda1e60be3fe845850a5 to your computer and use it in GitHub Desktop.
Django rename app management command
import argparse
from django.core.management.base import BaseCommand
from django.db import connection
class Command(BaseCommand):
help = 'Renames app. Usage rename_app [old_name] [new_name] [classes ...]'
def add_arguments(self, parser):
# Positional arguments
parser.add_argument('old_name', nargs=1, type=str)
parser.add_argument('new_name', nargs=1, type=str)
parser.add_argument('models', nargs=argparse.REMAINDER, type=str)
def handle(self, old_name, new_name, models, *args, **options):
with connection.cursor() as cursor:
# Rename model
old_name = old_name[0]
new_name = new_name[0]
cursor.execute("UPDATE django_content_type SET app_label='{}' WHERE app_label='{}'".format(new_name, old_name))
cursor.execute("UPDATE django_migrations SET app='{}' WHERE app='{}'".format(new_name, old_name))
for model in models:
cursor.execute("ALTER TABLE {old_name}_{model_name} RENAME TO {new_name}_{model_name}".format(
old_name=old_name, new_name=new_name, model_name=model))
@onekiloparsec
Copy link

onekiloparsec commented Jul 6, 2018

Following https://stackoverflow.com/questions/8408046/how-to-change-the-name-of-a-django-app?answertab=active#tab-top, I just applied this gist to one of my apps, Django 1.11 and it worked. Thanks ! (Make sure to include all model class names of the app).

@aleksandr-smechov
Copy link

aleksandr-smechov commented Dec 18, 2018

If you want to run this via python manage.py rename_app, place this in your app's folder before running: "your_app/management/commands/rename_app.py".

If you have any ManyToMany fields in your models, make sure to run the below lines for any model fields that have ManyToMany connections:

ALTER TABLE {old_name}_{model_name}_{m2m_relation_field} RENAME TO {new_name}_{model_name}_{m2m_relation_field}

Might be helpful to check out your db's tables after running the above snippet (if you're using postgres: sudo -u postgres psql your_db, then \dt)

@HilarioJuniorNengare
Copy link

"(django-ecommerc) D:\django-ecommerc>python manage.py rename OnlineShop
usage: manage.py rename [-h] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
current [current ...] new [new ...]
manage.py rename: error: the following arguments are required: new "

Guys I am receiving this error after I run this..how do I fix this. I am new to github and programming..

@cristianr909090
Copy link

python3 manage.py rename your_existing_app_name name_you_want

so for example:

python manage.py rename demo OnlineShop

here demo is your original app name, and OnlineShop is the new app name

@nithinexe
Copy link

python manage.py rename djecommerce

usage: manage.py rename [-h] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color]
[--force-color]
current [current ...] new [new ...]
manage.py rename: error: the following arguments are required: new

can anyone help me with this.

@hafizhmrsyd
Copy link

python manage.py rename demo OnlineShop

Big thanks! It works on my project

@xtlc
Copy link

xtlc commented May 11, 2021

python manage.py rename djecommerce

usage: manage.py rename [-h] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color]
[--force-color]
current [current ...] new [new ...]
manage.py rename: error: the following arguments are required: new

can anyone help me with this.

You need to set the "new" argument -> the name the app should get after rename.

@Mounaiim
Copy link

Mounaiim commented Aug 9, 2021

python3 manage.py rename your_existing_app_name name_you_want

so for example:

python manage.py rename demo OnlineShop

here demo is your original app name, and OnlineShop is the new app name

worked like a charm

@SebastianDevelops
Copy link

python manage.py rename djecommerce

usage: manage.py rename [-h] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color] current [current ...] new [new ...] manage.py rename: error: the following arguments are required: new

can anyone help me with this.

Just run this:

python manage.py rename demo djecommerce

Ultimately what you're doing is referring to which name you want to change and then supplying the new name

python manage.py rename old_name new_name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment