Django-代理模型权限

Abe*_*Abe 2 python django data-migration model

auth.Group想从模型创建一个代理模型,我想将其应用于permissions它。

代理

class InstitutionOwnerGroup(Group):

    # Gestores
    pk = 1

    is_superuser = False
    is_staff = False

    class Meta:
        proxy = True
        permissions = (
            ('can_manage_institutions', 'Gerencia Instituições'),
        )
Run Code Online (Sandbox Code Playgroud)

数据迁移

# Generated by Django 2.1.1 on 2018-10-14 23:30

from django.db import migrations
from horsy.apps.accounts.proxies import InstitutionOwnerGroup


def create_owner_group(app, _schema):
    InstitutionOwnerGroup.objects.create(
        name="Gestores"
    )


class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0002_add_admin_user'),
    ]

    operations = [
        migrations.RunPython(create_owner_group)
    ]
Run Code Online (Sandbox Code Playgroud)

管理面板中的Gestores模型:

在管理面板中分组

给定的权限can_manage_institutions未应用于数据迁移中的模型。

如何通过使用django权限系统将权限应用于继承自的代理模型auth.Group

sol*_*oke 5

这是一个已知问题最近有一个更新的拉取请求对其进行了修复,因此,运气好的话,它将在下一版本的Django中得到修复。

同时,上面的票证中有一些建议的解决方法。最简单的方法似乎是手动为指向您的代理模型的非托管模型创建迁移,然后将触发要创建的适当权限对象:

migrations.CreateModel(
    name='InstitutionOwnerGroup',
    fields=[
    ],
    options={
        'verbose_name': 'Group',
        'managed': False,  # Make it unmanaged
        'proxy': True,
        'verbose_name_plural': 'Groups',
    },
    bases=('myapp.proxies.group',),
    managers=[
        ('objects', django.contrib.auth.models.GroupManager()),
    ],
),
Run Code Online (Sandbox Code Playgroud)

您的里程可能与此有关-我不确定这样做是否完全安全。