简体   繁体   English

如何在Django 1.7迁移中引用生成的权限

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

I'm trying to create a auth.Group with permissions automaticly with migrations. 我正在尝试通过迁移自动创建具有权限的auth.Group。 My problem is that when I run migrate on empty database the migration that tries to attach permission to the group can't find the permission. 我的问题是,当我在空数据库上运行迁移时,尝试将权限附加到组的迁移找不到该权限。 If I target an earlier migration so that migrate exits without an error the permissions appear into the database and after that the migration code can find the permission. 如果我以较早的迁移为目标,以便迁移没有错误退出,则权限将显示在数据库中,然后迁移代码可以找到该权限。 So what can I do so that the migration can reference a permission which is created in an earlier migration when the migrations are run back to back? 那么,当迁移背对背运行时,该如何做才能使迁移能够引用在早期迁移中创建的权限?

def load_data(apps, schema_editor):
    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),
    ]

The game model is created in an earlier migration. 游戏模型是在较早的迁移中创建的。 This code causes always an error stating that Permission matching query does not exist when I run it with other migrations on an empty database. 此代码始终会导致错误,指出在空数据库上与其他迁移一起运行权限匹配查询时不存在。 I'm using python 3.4 with django 1.7.2 我在Django 1.7.2中使用python 3.4

Oh. 哦。 4 years later... To create permissions Django uses post_migrate signal. 4年后...为了创建权限,Django使用post_migrate信号。

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 : 不会通过create_permissions的检查:

if not app_config.models_module:
    return

But you have to be careful. 但是您必须要小心。

I hope someone will be useful. 我希望有人会有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM