How to add a permission to a user/group during a django migration?

EDIT 2018-01-31

This answer will only work until Django 1.9. For Django 1.10 an up, please refer to the answer provided by @anton-lisenkov

Original Answer (Django<1.10)

It turns out I could do the following:

from django.contrib.auth.management import create_permissions

def add_permissions(apps, schema_editor):
    apps.models_module = True

    create_permissions(apps, verbosity=0)
    apps.models_module = None

Thanks @elad-silver for his answer: https://stackoverflow.com/a/34272647/854868


AttributeError: 'StateApps' object has no attribute 'label' in Django 1.10

There is a solution:

for app_config in apps.get_app_configs():
    app_config.models_module = True
    create_permissions(app_config, verbosity=0)
    app_config.models_module = None