I have created a model and I want to create some groups and permissions dynamically with a specific field name of the model and the group name or permission code name.
For example, if I have Institute model with some fields (for example eiin, name, address, category), I want a dynamic custom permission called name_can_edit_address for the name field. Which is working fine (like-_can_edit_address) with Meta class but I can't add field name of the model as suffix of the permission(like name_can_edit_address) as code name.
It is possible to do that? It's get 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')
)