-
-
Save rafaponieman/201054ddf725cda1e60be3fe845850a5 to your computer and use it in GitHub Desktop.
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)) |
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
)
"(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..
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
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.
python manage.py rename demo OnlineShop
Big thanks! It works on my project
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: newcan anyone help me with this.
You need to set the "new" argument -> the name the app should get after rename.
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
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
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).