Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing the auth_permission table after renaming a model in Django

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

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

like image 455
shacker Avatar asked Feb 27 '09 07:02

shacker


2 Answers

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.

like image 159
Andy Baker Avatar answered Nov 09 '22 13:11

Andy Baker


If you happened to have used a South schema migration to rename the table, the following line in the forward migration would have done this automatically:

db.send_create_signal('appname', ['modelname'])
like image 42
Jian Avatar answered Nov 09 '22 13:11

Jian