Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Django admin permissions programmatically

The Django site I'm working on has the possibility for users to sign up for an account. To provide them with some editing functionality, I use the built-in Django admin. However, I'm having a problem: After a user has signed up, they don't have any permissions inside the Django admin, not even view permissions. Thus my question: How do I, in code, assign admin permissions to the user for the relevant models, in the same way I can assign them manually in the "User Permissions" section when editing the user in the admin? I've already tried with the usual has_xxx_permissions() using custom ModelAdmin classes, but that didn't work. So my guess is that I overlooked something obvious. Any ideas?

like image 392
Timo Avatar asked Jun 01 '12 15:06

Timo


People also ask

How do I give permission to user in Django?

With Django, you can create groups to class users and assign permissions to each group so when creating users, you can just assign the user to a group and, in turn, the user has all the permissions from that group. To create a group, you need the Group model from django. contrib. auth.

How do I restrict access to parts of Django admin?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

Does Django administrative permission?

The Django admin site uses permissions as follows: Access to view objects is limited to users with the “view” or “change” permission for that type of object. Access to view the “add” form and add an object is limited to users with the “add” permission for that type of object.


3 Answers

https://docs.djangoproject.com/en/dev/topics/auth/default/#permissions-and-authorization

new_user.user_permissions.add(permission1, permission2, etc...)
like image 101
dm03514 Avatar answered Oct 22 '22 18:10

dm03514


For your purposes, it would probably be much more easy and and efficient to assign all new users to a particular group, and then give that group all the permissions the user needs. Any member of the group will inherit those permissions as well.

You can create the group and assign the permissions to it in the admin. Then, you just need to add something like the following to your registration code.

try:
    group = Group.objects.get(name='The User Group')
except Group.DoesNotExist:
    # group should exist, but this is just for safety's sake, it case the improbable should happen
    pass
else:
    user.groups.add(group)
like image 27
Chris Pratt Avatar answered Oct 22 '22 20:10

Chris Pratt


dgel's answer pointed me in the direction which lead to a working solution for me. Essentially, what he seems to be suggesting is:

  1. Retrieve a ContentType for the model you want to set permissions for. In this context, a content type is an object that holds information about a Django model.
  2. Create a Permission object consisting of the content type and the action you want to allow inside the admin, using Permission.objects.get(). The only difficulty here is figuring out the codename parameter, which, for admin permissions, consists of an action ("add", "change" or "delete"), an underscore, and the model name. So if you have a model called Foo and you want to create all permissions for it, you'll need 3 permissions, each with the content type of your Foo model plus the code names add_foo, change_foo, and delete_foo.
  3. Assign these permissions using user.user_permissions.add(permission).

Head over to dgel's answers for code examples. Looking at a data dump of the auth app (manage.py dumpdata auth) of an existing Django database provided me with insights into the inner workings of permissions, too.

like image 29
Timo Avatar answered Oct 22 '22 18:10

Timo