-
-
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)) |
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
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.