How to add groups in Django using authentication system?


Django is equipped with built-in permissions system that assigns permissions to specific users or groups of users. Permissions used by the Django-admin site are as follows,

  • Users with the "view" or "update" permission for that type of object have access to view objects.

  • Only users with the "add" permission for that type of item have access to view the "add" form and add an object.

  • Users having the "change" permission for that type of item have access to the change list, the "change" form, and the ability to change an object.

  • Only users having the "delete" permission for that object type have access to delete it.

Permissions can be set for different types of objects and also to specific object instances. This can be done by using the has_add_permission(), has_view_permission(), has_change_permission() and has_delete_permission() methods.

These methods provided in the ModelAdmin class and using these methods, customization of permissions for different objects is possible.

#set user’s groups:
Myuser.groups =group_list

#to add or remove a user from a group the following commands can be used.
Myuser.groups.add(grp1,grp2)
Myuser.groups.remove(grp1,grp2)

#To add, remove and clear all permissions the following commands can be used.
Myuser.permissions=permission_list
Myuser.permissions.add(p1,p2,p3)
Myuser.permissions.remove(p1)
Myuser.permissions.clear()

All the above mentioned commands are usually written in the views.py file. In this file, the information entered by users is received and it is authenticated and authorized.

So the above mentioned codes should ideally be written in a views.py file in your project.

Default permissions are provided using django.contrib.auth which is listed in the INSTALLED_APPS in settings.py file. This will by default ensure that add, view, update and delete permissions are given to all the models in your project.

For example, you are running an ecommerce site and your website has customers and employees as its users, an app named review and a model named product. To check if a user has permissions regarding that model, the following lines of code can be used.

User.has_perm(‘review.add_product’)
User.has_perm(‘review.remove_product’)
User.has_perm(‘review.delete_product’)
User.has_perm(‘review.view_product’)

To check if a user has particular permissions instead of individually checking for all, the following can also be used.

From django.contrib.auth.decorators import permission_required
@permission_required(‘review.add_product’)
Def fun(request):
   ‘’’can raise error stating that permission is denied.’’’

Or

{% if perms.review.add_product %}

The above line can also be used to check if user has permission.

Custom permissions

In case the 4 permissions given by Django do not satisfy your website needs, you can create custom permissions. Such as a find product permissions. The customer and employee can both have the permission to search for a product.

To design a custom permission, you can add it in the product model permissions.

From django.db import models
Class Product(models.Model);
   User=models.ForeignKey(User)
   Class Meta:
      Permissions=(
         (“search_product”,”search for vote”),
      )

Groups in Django

Django groups is a list of permissions. A group consists of multiple users. One user can be a part of many groups and one group can have multiple users.

The major advantage of using group is that, a user in a group automatically has all the permissions given to that group. Multiple groups can be created to restrict permissions.

To create groups in Django the following can be performed.

From django.contrib.auth.models import Group
Employee_group,created=Group.objects.get_or_create(name=’employee’)
To assign a set of permissions to a particular group
Employee_group.permissions.set([list of permissions])
Employee_group.permissions.remove([list of permissions])
Employee_group.permissions.add([list of permissions])
Employee_group.permissions.clear()

Updated on: 05-Sep-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements