Go – Fixing the auth_permission table after renaming a model in Django

djangodjango-models

Every now and then, you have the need to rename a model in Django (or, in one recent case I encountered, split one model into two, with new/different names). Yes, proper planning helps to avoid this situation, but sometimes reality intervenes.

After renaming corresponding tables in the db and fixing affected code, one problem remains: Any permissions granted to Users or Groups to operate on those models still references the old model names. Is there any automated or semi-automated way to fix this, or is it just a matter of manual db surgery? (in development you can drop the auth_permissions table and syncdb to recreate it, but production isn't so simple).

Best Solution

Here's a snippet that fills in missing contenttypes and permissions. I wonder if it could be extended to at least do some of the donkey work for cleaning up auth_permissions.