[Django]-How to reference generated permissions in Django 1.7 migrations

3👍

Oh. 4 years later…
To create permissions Django uses post_migrate signal.

Therefore, when running all migrations at a time, permissions do not yet exist.

Therefore, you can take out your function, for example, in the management command.

However, you can still do it like this:

from django.contrib.auth.management import create_permissions


APPS = [
    ...your app labels
]


def create_applications_permissions():
    for app in APPS:
        app_config = django_apps.get_app_config(app)
        create_permissions(app_config)


def load_data(apps, schema_editor):
    create_applications_permissions()
    Permission  = apps.get_model('auth', 'Permission')
    Group       = apps.get_model('auth', 'Group')

    can_add = Permission.objects.get(codename='add_game')
    developers = Group.objects.create(name='Developer')

    developers.permissions.add(can_add)
    developers.save()


class Migration(migrations.Migration):

    dependencies = [
        ('myApp', '0004_game'),
    ]

    operations = [
        migrations.RunPython(load_data),
    ]

And to create permissions do not use apps passed to the migration. Will not pass the check in create_permissions:

if not app_config.models_module:
    return

But you have to be careful.

I hope someone will be useful.

Leave a comment