Permissions
- Understand Django permissions and how to implement role-based access control.
What are Permissions in Django?
Permissions define actions a user is allowed to perform.
Examples:
Can add a product
Can edit a blog post
Can delete a user
Django permissions are:
Model-based
Database-driven
Automatically created
Default Permissions in Django
For every model, Django automatically creates:
Example for Book model:
add_book
change_book
delete_book
view_book
Permission Architecture (Flow)
User โ Group โ Permissions โ Access Control
โ Users can have permissions directly
โ Users can inherit permissions from groupsGroups in Django
A Group is a collection of permissions.
Why use Groups?
Easy role management
Avoid assigning permissions one by one
Ideal for real-world roles (Admin, Editor, Staff)
Creating Models for Permission Demo
Sample Model
A simple Article model for demonstrating permissions.
# models.py
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
is_published = models.BooleanField(default=False)
def __str__(self):
return self.title
After creating model:
python manage.py makemigrations
python manage.py migrate
Working with Permissions in Code
Check User Permission
Use has_perm() to check permissions.
if request.user.has_perm('app_name.add_article'):
print("User can add article")
Creating Groups & Assigning Permissions (Admin Panel)
Steps:
Open Django Admin
Go to Groups
Create group (e.g., Editor)
Assign permissions
Add users to group
Example Groups:
Admin โ All permissions
Editor โ Add & change articles
Viewer โ View only
Programmatically Creating Groups
Create Group with Permissions
Create a group and assign permissions using ORM.
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from .models import Article
content_type = ContentType.objects.get_for_model(Article)
permissions = Permission.objects.filter(content_type=content_type)
editor_group, created = Group.objects.get_or_create(name='Editor')
editor_group.permissions.set(permissions)
User Roles (Role-Based Access Control)
Django does not provide roles directly, but we implement roles using:
Groups
Permissions
Custom logic
Common Roles:
Restricting Views Using Permissions
Permission Protected View
Use decorator to restrict access.
from django.contrib.auth.decorators import permission_required
@permission_required('app_name.add_article', raise_exception=True)
def add_article(request):
return HttpResponse("You can add article")
Permissions in Templates
Template Permission Check
Show buttons conditionally.
{% if perms.app_name.change_article %}
<button>Edit</button>
{% endif %}
Superuser vs Staff
Importance of Permissions
โ Secure application
โ Prevent unauthorized actions
โ Scalable user management
โ Industry-standard access controlRealโWorld Example
College Management System
Admin โ Manage users, courses
Teacher โ Add marks, update syllabus
Student โ View results only