Search code examples
djangopermissionsadminmodels

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!


Solution

  • 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.