Skip to content

Instantly share code, notes, and snippets.

@arnaudbos
Last active December 17, 2015 01:59
Show Gist options
  • Save arnaudbos/5532440 to your computer and use it in GitHub Desktop.
Save arnaudbos/5532440 to your computer and use it in GitHub Desktop.
Create permissions or anything else after syncdb of an app has finished.
This should be located in: yourapp/management.py
from django.db.models.signals import post_syncdb
from django.contrib.auth import models as auth_models
from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
def create_supervision_permissions(sender, **kwargs):
print '== create_supervision_permissions =='
# create a content type for the app if it doesn't already exist
ct, created = ContentType.objects.get_or_create(model = '', app_label = 'Supervision', defaults = {'name': 'Supervision'})
# create a permission if it doesn't already exist
can_view_statistics = Permission.objects.get_or_create(
codename = 'can_view_statistics',
content_type__pk = ct.id, defaults = {'name': 'Can view statistics', 'content_type': ct} )
# Run create_supervision_permissions just after syncdb has finished to install the models of the auth app.
post_syncdb.connect(create_supervision_permissions, sender=auth_models)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment