MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
User Model & Permissions0%
Study Materials
Django for Backend DevelopmentProgress 0%
1.1Installing Django & Environment Setup1.2Django Project Structure1.3Creating First Django App
3.1Django Admin Site3.2User Model & Permissions3.3Groups and Roles
HomeTutorialsDjango for Backend DevelopmentUser Model & Permissions
Groups and Roles

User Model & Permissions

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

User authentication and authorization are mission-critical for web platforms. While Django ships with a default User model, production architectures strongly advise configuring a Custom User Model at the inception of a project. This allows you to use email addresses as unique login identifiers, add custom enterprise fields, and manage fine-grained permissions.


1. Why You MUST Use a Custom User Model

Django's default auth.User uses username for logins and has fixed fields (first_name, last_name, email). Altering the user model mid-project requires complex, risky database migrations across foreign keys.

By defining a custom user model inheriting from AbstractUser on Day 1, you preserve total freedom to extend user attributes throughout the application lifetime:

1# accounts/models.py
2from django.contrib.auth.models import AbstractUser
3from django.db import models
4 
5class CustomUser(AbstractUser):
6 # Enforce unique email address as primary login identifier
7 email = models.EmailField(unique=True)
8
9 # Custom business attributes
10 bio = models.TextField(blank=True)
11 company_name = models.CharField(max_length=100, blank=True)
12 is_verified = models.BooleanField(default=False)
13 avatar = models.ImageField(upload_to="avatars/", null=True, blank=True)
14 
15 # Use email instead of username for authentication
16 USERNAME_FIELD = "email"
17 REQUIRED_FIELDS = ["username"] # Fields prompted during createsuperuser
18 
19 def __str__(self):
20 return self.email

2. Registering AUTH_USER_MODEL in Settings

Inform Django to use your custom model globally:

1# config/settings.py
2AUTH_USER_MODEL = "accounts.CustomUser"

When referencing the user model across your codebase:

  • In models.py foreign keys: use settings.AUTH_USER_MODEL
  • In view code: use get_user_model()
1# In models.py:
2author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
3 
4# In views.py:
5from django.contrib.auth import get_user_model
6User = get_user_model()

3. Django Permissions Framework

Django models automatically generate four default permissions upon creation:

  1. 1
    add_<modelname> (e.g. articles.add_article)
  2. 2
    change_<modelname> (e.g. articles.change_article)
  3. 3
    delete_<modelname> (e.g. articles.delete_article)
  4. 4
    view_<modelname> (e.g. articles.view_article)

Checking Permissions Programmatically

1# Check permission on a user instance
2if user.has_perm("articles.delete_article"):
3 print("User is authorized to delete this article")

Guarding Views with @permission_required

1from django.contrib.auth.decorators import permission_required
2 
3@permission_required("articles.delete_article", raise_exception=True)
4def delete_article_view(request, article_id):
5 # Only users with explicit delete permissions can execute this view!
6 ...

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why does Django's official documentation recommend creating a Custom User Model at the very beginning of a new project?
Question 2
What setting in settings.py tells Django to adopt your custom user model?
Question 3
How should you reference the User model when defining ForeignKey relationships in other models?
Question 4
What four permissions does Django automatically generate for every registered database model?
Question 5
How do you configure a custom user model to log in using an email address instead of a username?
Next Lesson

Groups and Roles

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Django Admin SiteGroups and Roles

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Django for Backend Development with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext