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.

#To add, remove and clear all permissions the following commands can be used.

All the above mentioned commands are usually written in the 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 file in your project.

Default permissions are provided using django.contrib.auth which is listed in the INSTALLED_APPS in 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.


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
Def fun(request):
   ‘’’can raise error stating that permission is denied.’’’


{% if %}

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);
   Class Meta:
         (“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
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])