在 Django 中为模型的特定字段名称动态创建自定义权限
Create custom permission dynamically for specific field name of a model in Django
我已经创建了一个模型,我想使用模型的特定字段名称和组名称或权限代码名称动态创建一些组和权限。
例如,如果我的 Institute 模型有一些字段(例如 eiin、name, address, category), 我想要一个名为 name_can_edit_address 的动态自定义权限对于 name 字段。 Meta class 工作正常(如-_can_edit_address)但我无法将模型的字段名称添加为权限的后缀(如 name_can_edit_address) 作为代号。
可以吗?它得到 NameError: name 'self' is not defined.
# model specifications
class Institute(models.Model):
CATEGORY=(
('play', 'play'),
('High school', 'High school'),
('College', 'College'),
)
eiin = models.CharField(max_length=50, blank=True, null= True)
name = models.CharField(max_length=100)
address = models.ForeignKey(Address, on_delete=SET_NULL, blank=True, null=True)
category = models.CharField(max_length=100, choices=CATEGORY)
# dynamic group creation ok
# create group dynamically with the model objects
def save(self, *args, **kwargs):
super(Institute, self).save(*args, **kwargs)
Group.objects.create(name=str(self.name)+"_students")
Group.objects.create(name=str(self.name)+"_teachers")
Group.objects.create(name=str(self.name)+"_controller")
Group.objects.create(name=str(self.name)+"_employees")
Group.objects.create(name=str(self.name)+"_guardians")
# problem is here. It's not taking name field as suffix of code name for permissions
# create permissions dynamically with institution name & permission code like (field_permissioncode)
class Meta:
permissions = (
(str(self.name)+'_can_edit_address',
'can edit address of institute'),
(str(self.name)+'_can_add_eiin',
'can add eiin of institute')
)
Django 权限是对象,然后他们有一个专用的class:Permission
。
但在 Django 中,权限链接到 ContentType
。
如果你想即时创建一些权限,你需要创建内容类型并在权限之后像这样:
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def save(self, *args, **kwargs):
super(Institute, self).save(*args, **kwargs)
Group.objects.create(name=str(self.name)+"_students")
# ...
# Create contenttype and permissions, replace app_label and model to fit your needs
content_type = ContentType.objects.get(app_label='app_name', model='Institute')
permission = Permission.objects.create(codename=str(self.name)+'_can_edit_address',
name='can edit address of institute',
content_type=content_type)
permission = Permission.objects.create(codename=str(self.name)+'_can_add_eiin',
name='can edit eiin of institute',
content_type=content_type)
您可以在
上找到更多解释
我已经创建了一个模型,我想使用模型的特定字段名称和组名称或权限代码名称动态创建一些组和权限。
例如,如果我的 Institute 模型有一些字段(例如 eiin、name, address, category), 我想要一个名为 name_can_edit_address 的动态自定义权限对于 name 字段。 Meta class 工作正常(如-_can_edit_address)但我无法将模型的字段名称添加为权限的后缀(如 name_can_edit_address) 作为代号。
可以吗?它得到 NameError: name 'self' is not defined.
# model specifications
class Institute(models.Model):
CATEGORY=(
('play', 'play'),
('High school', 'High school'),
('College', 'College'),
)
eiin = models.CharField(max_length=50, blank=True, null= True)
name = models.CharField(max_length=100)
address = models.ForeignKey(Address, on_delete=SET_NULL, blank=True, null=True)
category = models.CharField(max_length=100, choices=CATEGORY)
# dynamic group creation ok
# create group dynamically with the model objects
def save(self, *args, **kwargs):
super(Institute, self).save(*args, **kwargs)
Group.objects.create(name=str(self.name)+"_students")
Group.objects.create(name=str(self.name)+"_teachers")
Group.objects.create(name=str(self.name)+"_controller")
Group.objects.create(name=str(self.name)+"_employees")
Group.objects.create(name=str(self.name)+"_guardians")
# problem is here. It's not taking name field as suffix of code name for permissions
# create permissions dynamically with institution name & permission code like (field_permissioncode)
class Meta:
permissions = (
(str(self.name)+'_can_edit_address',
'can edit address of institute'),
(str(self.name)+'_can_add_eiin',
'can add eiin of institute')
)
Django 权限是对象,然后他们有一个专用的class:Permission
。
但在 Django 中,权限链接到 ContentType
。
如果你想即时创建一些权限,你需要创建内容类型并在权限之后像这样:
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
def save(self, *args, **kwargs):
super(Institute, self).save(*args, **kwargs)
Group.objects.create(name=str(self.name)+"_students")
# ...
# Create contenttype and permissions, replace app_label and model to fit your needs
content_type = ContentType.objects.get(app_label='app_name', model='Institute')
permission = Permission.objects.create(codename=str(self.name)+'_can_edit_address',
name='can edit address of institute',
content_type=content_type)
permission = Permission.objects.create(codename=str(self.name)+'_can_add_eiin',
name='can edit eiin of institute',
content_type=content_type)
您可以在