Skip to content

Instantly share code, notes, and snippets.

@sjquant
Created July 14, 2019 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjquant/23c682e19fd053e94d93179a35a9efaf to your computer and use it in GitHub Desktop.
Save sjquant/23c682e19fd053e94d93179a35a9efaf to your computer and use it in GitHub Desktop.
script to change model names
# Fork from https://github.com/amirraouf/change-django-app-name/blob/master/change_app_name.py
from django.contrib.auth.models import Permission, Group
from django.contrib.contenttypes.models import ContentType
from django.db import connection
from django.db.models import Q
ContentType.objects.filter(app_label='<old_app_name>').update(app_label='<app_name>') # change content type to prevent django migrations from re-migrate
permissions = Permission.objects.filter(Q(content_type__app_label='url_integrations') |
Q(content_type__app_label='pcm'))
admin_grp = Group.objects.get(name='<grp_name>') # add new permissions if changed or deleted
for p in permissions:
admin_grp.permissions.add(p)
with connection.cursor() as c:
c.execute("update django_migrations set app='<app_name>' where app='<old_app_name>';") # update django migrations table to set new app name
c.execute("select relname from pg_stat_user_tables where relname like '<old_app_name>%';") # select all tables of database to rename it
data = c.fetchall()
for d in data:
name = d[0]
name = name.split("<old_app_name>_")[1]
c.execute("ALTER table %s RENAME to <app_name>_%s;" % (d[0], name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment