python - How can dynamically create permission in django? -




now can create new groups using django group module. from django.contrib.auth.models import group can assign permissions group. example created new group "hr"

by group(name="hr") .

now want create permission

  • can_create_hr
  • can_edit_hr

i should able assign permission groups.

how can ?

you can create , assign permission directly groups well. create permission add permission group

from django.contrib.auth.models import user, group, permission django.contrib.contenttypes.models import contenttype  content_type = contenttype.objects.get(app_label='app_name', model='model_name') permission = permission.objects.create(codename='can_create_hr',                                        name='can create hr',                                        content_type=content_type) # creating permissions group = group.objects.get(name='hr') group.permissions.add(permission) 

if want assign permission group permission object , assign in same way.

permission = permission.objects.get(codename='can_create_hr') group= group.objects.get(name='some_name') group.permissions.add(permission) 

you can read more in docs





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -