So much for a post title.
The thing is as follows:
- you use migrations for a certain app
- in some point at your development, you add proxy models to that app
- you need to have specific permissions for that recently created proxy model.
- since that app you’re adding the proxy model is not controlled by the syncdb command of django (since it’s on migrations), you can not rely on django’s syncdb command to fix the permissions associated to that model for you.
- if you try to run the ‘migrate’ command, nothing happens, no permissions are added, neither any record in django_content_type table.
- what’s worst. if you reset the database and start over, then , since migration 0001_initial, the permissions are already there: however , you can not rely on this since you’ve already have a production site going on.
- You can not put the data in a data migration either, because, in case the migration is run from 0001 point when the fake models are already defined, it will throw an error when reaching a data migration that tries to reinsert the same data.
Well , what I find as an acceptable solution is the use of send_create_signal in a “fake” migration.
say you have an app which already have 10 migrations run on it. Now you add a proxy model. you create a “fake” schema migration whose only purspose is to send the send_create_signal so the django framework reacts and create the needed content_type and permission records .
In your models.py:
from django.db import models class TestModel(models.Model): .. class ProxyTestModel(TestModel): class Meta: proxy=True
and now create an empty schemamigration. this is where you’ll fire the “send_create_signal” event:
class Migration(SchemaMigration):
def forwards(self, orm):
# faking proxy model creation
db.send_create_signal('myapp', ['ProxyTestModel'])
def backwards(self, orm):
pass
when you run that migration, it will throw the send_create_signal associated to the model “ProxyTestModel” , in the app “myapp” . the django framework will recognize that and create if they are not created already, the necessary content_type and permissions records on the db.
2 Comments. Posted by jzarate on Tuesday, April 19, 2011 at 7:19 pm.
Filed under Bug solving, Django, django, migration, permission, proxy model, quick&dirty.
2 Comments on “Django admin permissions and contenttype table doesn’t update when adding proxy models”
You can track this conversation through its atom feed.
Works for me, thanks!
Posted on April 26, 2011 at 9:04 pm.
Thank you. Your post solved my problem.
Posted on June 3, 2011 at 12:27 pm.