Adding new custom permissions in Django

I am using custom permissions in my Django models like this:

class T21Turma(models.Model):
    class Meta:
        permissions = (("can_view_boletim", "Can view boletim"),
                       ("can_view_mensalidades", "Can view mensalidades"),)

The problem is that when I add a permission to the list it doesn't get added to the auth_permission table when I run syncdb. What am I doing wrong. If it makes any difference I am using south for database migrations.


South does not track django.contrib.auth permissions. See ticket #211 for more information.

One of the comments on the ticket suggests that using the --all option on syncdb may solve the problem.


If you want "manage.py migrate" to do everything (without calling syncdb --all). You need to create new permissions with a migration:

user@host> manage.py datamigration myapp add_perm_foo --freeze=contenttypes --freeze=auth

Edit the created file:

class Migration(DataMigration):

    def forwards(self, orm):
        "Write your forwards methods here."
        ct, created = orm['contenttypes.ContentType'].objects.get_or_create(
            model='mymodel', app_label='myapp') # model must be lowercase!
        perm, created = orm['auth.permission'].objects.get_or_create(
            content_type=ct, codename='mymodel_foo', defaults=dict(name=u'Verbose Name'))

This worked for me:

./manage.py update_permissions

It is a django-extensions thing.


You can connect to the post_migrate signal in order to update the permissions after migration. I use the following code, slightly modified from Dev with Passion and originally from django-extensions.

# Add to your project-level __init__.py

from south.signals import post_migrate

def update_permissions_after_migration(app,**kwargs):
    """
    Update app permission just after every migration.
    This is based on app django_extensions update_permissions management command.
    """
    from django.conf import settings
    from django.db.models import get_app, get_models
    from django.contrib.auth.management import create_permissions

    create_permissions(get_app(app), get_models(), 2 if settings.DEBUG else 0)

post_migrate.connect(update_permissions_after_migration)