Django - 代理模型的权限

Django - Permissions to proxy model

我从 auth.Group 模型创建了一个代理模型,我想对其应用 permissions

proxies.py

class InstitutionOwnerGroup(Group):

    # Gestores
    pk = 1

    is_superuser = False
    is_staff = False

    class Meta:
        proxy = True
        permissions = (
            ('can_manage_institutions', 'Gerencia Instituições'),
        )

数据迁移

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

管理面板中的 Gestores 模型:

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

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

这是一个 known issue. There is a recently updated pull request 修复程序,如果运气好的话它将在下一版本的 Django 中修复。

与此同时,上面的票证中有一些建议的解决方法。对于指向您的代理模型的非托管模型,最简单的似乎是manually create a migration,然后它将触发创建适当的权限对象:

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

您的里程可能会因此而有所不同 - 我不确定这样做是否完全安全。