如何在 Django 迁移期间向 user/group 添加权限?

How to add a permission to a user/group during a django 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)
    ]

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

我可以确认缺少的权限似乎只有在执行完所有迁移后才会添加。

编辑 2018-01-31

此答案仅适用于 Django 1.9。 Django 1.10及以上版本请参考@anton-lisenkov提供的

原始答案 (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 的回答:

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

如果您不必将您的许可附加到个人模型上,您可以这样做:

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