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

3

15

I would like to execute the following migration:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.models import Permission
from django.db import migrations
from django.conf import settings
from django.contrib.auth.models import Group, User


def add_api_group(apps, schema_editor):

    Group.objects.create(name=settings.API_USER_GROUP)
    # get_or_create returns a tuple, not a Group
    group = Group.objects.get(name=settings.API_USER_GROUP)
    permissions = Permission.objects.filter(codename__in = [
        'add_topic',
    ])
    group.permissions.add(*permissions)


def add_api_user(apps, schema_editor):

    user = User.objects.create_user(username=settings.API_USER, password=settings.API_USER_PASSWORD)
    group = Group.objects.get(name=settings.API_USER_GROUP)
    user.groups.add(group)

class Migration(migrations.Migration):

    dependencies = [
        ('nd_content', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(add_api_group),
        migrations.RunPython(add_api_user)
    ]

At the last line of the migration, I issued an error to stop execution and look at the database state. The problem is the table auth_permission still has not the permissions of a model of another module, although this other module is registered as a dependecy of this migration.

I can confirm missing permissions seem to be added only after all migrations have been executed.

Maribeth answered 8/8, 2016 at 6:9 Comment(0)
L
21
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
Lockridge answered 10/1, 2017 at 8:12 Comment(1)
django.contrib.auth.management.create_permissions sourceK
M
4

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://mcmap.net/q/589358/-django-data-migrate-permissions

Maribeth answered 8/8, 2016 at 7:5 Comment(0)
C
0

If you don't have to attach your permission to a personal model you can do it this way:

from django.contrib.auth.models import Permission, ContentType


def add_permission(apps, schema_editor):
    content_type = ContentType.objects.get(app_label='auth', model='user')  # I chose user model but you can edit it
    permission = Permission(
        name='Your permission description',
        codename='your_codename',
        content_type=content_type,
    )
    permission.save()

Captive answered 30/4, 2019 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.