Home > Mobile >  Create custom permission dynamically for specific field name of a model in Django
Create custom permission dynamically for specific field name of a model in Django

Time:11-17

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')
        )



CodePudding user response:

Django permissions are objects, then they have a dedicated class : Permission. But in Django, permission are linked to ContentType. If you want to create some permission on the fly, you need to create the contenttype and after the permission like this :

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)

You can find more explanations on this post

  • Related