如何在django迁移期间向用户/组添加权限?

时间:2016-08-08 06:09:36

标签: django django-models django-migrations django-permissions

我想执行以下迁移:

# -*- 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)
    ]

在迁移的最后一行,我发出错误以停止执行并查看数据库状态。问题是表auth_permission仍然没有另一个模块的模型的权限,尽管这个其他模块被注册为此迁移的依赖。

我可以确认只有在执行了所有迁移后才会添加丢失的权限。

3 个答案:

答案 0 :(得分:13)

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

有一个解决方案:

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

答案 1 :(得分:4)

编辑2018-01-31

此答案仅适用于Django 1.9。对于Django 1.10,请参考@ anton-lisenkov提供的answer

原始答案(Django< 1.10)

事实证明我可以做到以下几点:

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

谢谢@ elad-silver的回答:https://stackoverflow.com/a/34272647/854868

答案 2 :(得分:0)

如果您不必将许可权附加到个人模型,则可以通过以下方式做到这一点:

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()