You can expand your .add() statement slightly, by passing it an object
instead of an id:

request.user.user_permissions.add(Permission.objects.get(name='some
permission name'))

You could alternatively do a lookup with the "codename='some_name'"
instead of the human-readable name.  The codename is arguably less
prone to change than the straight "name" parameter.

Just be sure to import the Permission model.  It in
django.contrib.auth.models

Tim

On Nov 26, 2:59 am, Adonis <achrysoch...@hotmail.com> wrote:
> Hi,
>
> I have been going through the permission documentation and i know i
> can assign users with permissions like this,
>
> *
> request.user.user_permissions.add(1)
> *
>
> But having 100 or more permissions i find this method a bit weird. Is
> not there a way to say,
>
> *
> request.user.user_permissions.add('auth.delete_permission') or what i
> found in an other post,
> *
>
> *
> def get_permission_object(perm_label):
>     from django.contrib.contenttypes.models import ContentType
>     from django.contrib.auth.models import Permission
>     app_label,perm_code_name = perm_label.split('.')
>     content_type = ContentType.objects.get(app_label=app_label)
>     perm = Permission.objects.get
> (content_type=content_type,codename=perm_code_name)
>     return perm
>
> def add_perm_to_user(user,perm_label):
>     user.user_permissions.add(get_permission_object(perm_label))
>     user.save()
>     return user
> *
>
> Any suggestions?
> Thanks in advance!

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to