如何在 Django 抽象模型类中动态命名权限?

发布于 2024-10-17 01:23:58 字数 706 浏览 3 评论 0原文

我想在抽象模型类上定义一些自定义权限,然后由所有子类继承,而不是为权限提供可应用于任何子类模型类型的通用对象名称,我想本质上使用verbose_name_plural 属性作为权限名称和描述的一部分(例如 ('view_classname', 'Can view classname')),模拟 Django 的默认行为。

所以,我希望做的是这样的事情(这是行不通的,因为在这种情况下没有定义 verbose_name_plural ):(

class AbstractModel(models.Model):
    class Meta:
        abstract = True
        permissions = (
            (u'view_%ss' % verbose_name_plural, u'Can view %s' % verbose_name_plural),
        )

这个问题也被描述在 http://code.djangoproject.com/ticket/10686,其中包含实现动态替换 的补丁%(class)s 在权限定义中,但是这个补丁从未被接受,并且我的生产环境不允许修补 Django。)

I want to define some custom permissions on an abstract model class that would then be inherited by all child classes, and rather than give the permissions a generic object name that could apply to any subclassed model type, I would like to essentially use the verbose_name_plural property of the child model as part of the permission names and description (e.g. ('view_classname', 'Can view classname')), emulating Django's default behavior.

So, what I would be hoping to do would be something like this (which doesn't work, since verbose_name_plural is not defined in this context):

class AbstractModel(models.Model):
    class Meta:
        abstract = True
        permissions = (
            (u'view_%ss' % verbose_name_plural, u'Can view %s' % verbose_name_plural),
        )

(This problem is also described at http://code.djangoproject.com/ticket/10686, which includes a patch that implements dynamic replacement of %(class)s in permission definitions, but this patch was never accepted and my production environment does not allow for patching Django.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦行七里 2024-10-24 01:23:58

您可以使用类装饰器而不是抽象模型类来完成此任务吗?

def with_view_perm(cls):
    vn = cls.Meta.verbose_name_plural
    perms = (('view_%s' % vn, 'Can view %s' % vn),)
    cls.Meta.perms += perms
    return cls

@with_view_perm
class Child(models.Model):
    class Meta:
        verbose_name_plural = 'children'
        perms = (('change_children', 'Can change children'),)

Could you accomplish this with a class decorator instead of an abstract model class?

def with_view_perm(cls):
    vn = cls.Meta.verbose_name_plural
    perms = (('view_%s' % vn, 'Can view %s' % vn),)
    cls.Meta.perms += perms
    return cls

@with_view_perm
class Child(models.Model):
    class Meta:
        verbose_name_plural = 'children'
        perms = (('change_children', 'Can change children'),)
黄昏下泛黄的笔记 2024-10-24 01:23:58

它很旧 - 但为了将来的参考 - 现在所需的行为是开箱即用的(Django 1.9)

考虑这个具有适当权限的抽象模型:

class DetailContentLifecycleClassModel (models.Model):
    class Meta:
        abstract=True
        permissions = (
            ('can_change_content', 'Change content of the model'),
            ('can_submit_for_approval', 'Ask for final check and publishing'),
            ('can_publish_content', 'Publish the model as a new version'),
        )

当像这样继承时:

class Test_Details (DetailContentLifecycleClassModel):
    name = models.CharField(max_length=200)

class Test_Details2 (DetailContentLifecycleClassModel):
    name = models.CharField(max_length=200)

权限创建如下:

from playground.models import Test_Details
from django.contrib.auth.models import User, Permission

tmp = Permission.objects.filter()

结果(这正是原来的内容)通缉):

playground | test_ details | Can add test_ details
playground | test_ details | Change content of the model
playground | test_ details | Publish the model as a new version
playground | test_ details | Ask for final check and publishing
playground | test_ details | Can change test_ details
playground | test_ details | Can delete test_ details
playground | test_ details2 | Can add test_ details2
playground | test_ details2 | Change content of the model
playground | test_ details2 | Publish the model as a new version
playground | test_ details2 | Ask for final check and publishing
playground | test_ details2 | Can change test_ details2
playground | test_ details2 | Can delete test_ details2

It's old - but for future reference - the desired behaviour is working out of the box now (Django 1.9)

Consider this abstract model with the appropriate permissions:

class DetailContentLifecycleClassModel (models.Model):
    class Meta:
        abstract=True
        permissions = (
            ('can_change_content', 'Change content of the model'),
            ('can_submit_for_approval', 'Ask for final check and publishing'),
            ('can_publish_content', 'Publish the model as a new version'),
        )

When inheriting like this:

class Test_Details (DetailContentLifecycleClassModel):
    name = models.CharField(max_length=200)

class Test_Details2 (DetailContentLifecycleClassModel):
    name = models.CharField(max_length=200)

The Permssions are created as following:

from playground.models import Test_Details
from django.contrib.auth.models import User, Permission

tmp = Permission.objects.filter()

Result (which is exactly what was wanted):

playground | test_ details | Can add test_ details
playground | test_ details | Change content of the model
playground | test_ details | Publish the model as a new version
playground | test_ details | Ask for final check and publishing
playground | test_ details | Can change test_ details
playground | test_ details | Can delete test_ details
playground | test_ details2 | Can add test_ details2
playground | test_ details2 | Change content of the model
playground | test_ details2 | Publish the model as a new version
playground | test_ details2 | Ask for final check and publishing
playground | test_ details2 | Can change test_ details2
playground | test_ details2 | Can delete test_ details2
深海里的那抹蓝 2024-10-24 01:23:58

另一种较新的方法是在基类的 Meta 中设置 default_permissions。

另请注意,执行此操作时,您需要进行并运行迁移才能使其生效。

The other newer way of doing this is to set default_permissions in the Meta on your base class.

Also be aware that when doing that you to make and run migrations for it to take effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文