Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing a user to access specific models in admin

If I had a model similar to this:

class Dealer(models.Model):

    name        = models.CharField(max_length=10)
    other_name  = models.CharField(max_length=10,  blank=True, null=True)
... etc.

If I wanted to create a user that can sign in to the admin site and only have access to edit this model only, how could I go about doing this?

I have looked around and haven't been able to successfully get it to work. I have also looked into packages such as guardian but had no success. What am I missing here?

Thank you!

like image 451
Shawn Avatar asked May 27 '14 18:05

Shawn


People also ask

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 .

How do I give permission to a specific user in Django?

through django-adminopen your django-admin page and head to Users section and select your desired user . NOTE: Permission assigning process is a one-time thing, so you dont have to update it every time unless you need to change/re-assign the permissions.

Which types of users are allowed to login to the Django Administration site?

Only one class of user exists in Django's authentication framework, i.e., 'superusers' or admin 'staff' users are just user objects with special attributes set, not different classes of user objects.

How do I add permissions to a Django model?

Add Permissions to a Group If you are using AbstractUser in Django, you must add AUTH_USER_MODEL = 'YourAppName. YourClassName' . This way, you are telling Django to use our custom user model instead of the default one. The code below should go in your admin.py file so that you can see your user model.


1 Answers

You can deal with permissions to allow him to only see this model. Each user can be assigned a group, where a group has a limited set of permissions and a user can also have a set of permissions available. You just need to put this user as a "Staff" user (is_staff = 1) and then put him every permission about the Dealer model.

Admin permissions right for one user

If the user isn't a superuser, he will see only links to the Dealer model. Everything about permissions and authorization are presented in the Django documentation.

The Django admin site uses permissions as follows:

  • Access to view the “add” form and add an object is limited to users with the “add” permission for that type of object.
  • Access to view the change list, view the “change” form and change an object is limited to users with the “change” permission for that type of object.
  • Access to delete an object is limited to users with the “delete” permission for that type of object.

Permissions can be set not only per type of object, but also per specific object instance. By using the has_add_permission(), has_change_permission() and has_delete_permission() methods provided by the ModelAdmin class, it is possible to customize permissions for different object instances of the same type.

like image 103
Maxime Lorant Avatar answered Jan 18 '23 19:01

Maxime Lorant