如何从元类中访问类属性

时间:2019-07-18 04:15:10

标签: django model permissions

我正在向模型添加字段级权限。我想将它们与其他对象级别的权限分开,以便我的代码可以检查是否已为任何给定字段定义了权限,但是我仍然想合并它们,以便在auth_permission中创建字段级别的权限。桌子。

我首先尝试在Meta类中创建field_permissions,但遇到“类Meta具有无效属性”错误。

然后我将field_permissions作为模型类属性移出了Meta类,并尝试将其附加到“权限”(在Meta类中定义),但是我在Meta类内部获得了关于field_permissions的“未解决的参考” 。我也尝试过self.field_permissions,但这也不起作用。

这是我的模特:

class Client(AddressPhoneModelMixin, DateFieldsModelMixin, models.Model):
    name = models.CharField(
        verbose_name=_('Client'),
        max_length=100,
    )
    status = models.CharField(
        verbose_name=_('Status'),
        max_length=25,
    )

    field_permissions = (
        ('view_client_id', 'Can view client ID'),
        ('view_client_name', 'Can view client name'),
        ('view_client_address', 'Can view client address'),
        ('view_client_country', 'Can view client country'),
        ('view_client_status', 'Can view client status'),
        ('view_client_phone_number', 'Can view client phone number'),
        ('view_client_mobile_number', 'Can view client mobile number'),
        ('view_client_fax_number', 'Can view client fax number'),
        ('view_client_created', 'Can view client created'),
        ('view_client_updated', 'Can view client updated'),
        ('view_client_deleted', 'Can view client deleted'),
        ('change_client_id', 'Can change client ID'),
        ('change_client_name', 'Can change client name'),
        ('change_client_address', 'Can change client address'),
        ('change_client_country', 'Can change client country'),
        ('change_client_status', 'Can change client status'),
        ('change_client_phone_number', 'Can change client phone number'),
        ('change_client_mobile_number', 'Can change client mobile number'),
        ('change_client_fax_number', 'Can change client fax number'),
        ('change_client_created', 'Can change client created'),
        ('change_client_updated', 'Can change client updated'),
        ('change_client_deleted', 'Can change client deleted'),
    )

    class Meta:
        verbose_name        = "client"
        verbose_name_plural = "clients"
        ordering            = ('created',)

        permissions = (
            ('view_all_clients', 'Can view all clients'),
            ('archive_client', 'Can archive client'),
        )
        permissions += field_permissions        <-- field_permissions is an unresolved reference

我想做的甚至有可能吗?如果没有,为什么?

0 个答案:

没有答案