views:

265

answers:

2

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

A: 

I got about half-way through a long answer that detailed the plan of attack I would take in this situation, but as I was writing I realized there probably isn't any way around having to do a maintenance downtime in this situation.

You can minimize the downtime by having a prepared loaddata script of course, although care needs to be taken to make sure the auth_perms primary keys are in sync.

Also see short answer: no automated way to do this of which I'm aware.

Van Gale
+2  A: 

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.

andybak
Note this version of the script does not work with Django 1.0 - use http://www.djangosnippets.org/snippets/696/ instead.
shacker