├── README.md └── change_app_name.py /README.md: -------------------------------------------------------------------------------- 1 | # change-django-app-name 2 | Snippet to change django app name 3 | According to `https://stackoverflow.com/a/8408131/1690893` 4 | -------------------------------------------------------------------------------- /change_app_name.py: -------------------------------------------------------------------------------- 1 | from django.contrib.auth.models import Permission, Group 2 | from django.contrib.contenttypes.models import ContentType 3 | 4 | from django.db import connection 5 | from django.db.models import Q 6 | 7 | 8 | ContentType.objects.filter(app_label='').update(app_label='') # change content type to prevent django migrations from re-migrate 9 | permissions = Permission.objects.filter(Q(content_type__app_label='app_label1') | 10 | Q(content_type__app_label='app_label2')) 11 | admin_grp = Group.objects.get(name='') # add new permissions if changed or deleted 12 | for p in permissions: 13 | admin_grp.permissions.add(p) 14 | with connection.cursor() as c: 15 | c.execute("update django_migrations set app='' where app='';") # update django migrations table to set new app name 16 | c.execute("select relname from pg_stat_user_tables where relname like '%';") # select all tables of database to rename it 17 | data = c.fetchall() 18 | for d in data: 19 | name = d[0] 20 | name = name.split("_")[1] 21 | c.execute("ALTER table %s RENAME to _%s;" % (d[0], name)) 22 | --------------------------------------------------------------------------------