User Model & Permissions
User Model & Permissions
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:
2. Registering AUTH_USER_MODEL in Settings
Inform Django to use your custom model globally:
When referencing the user model across your codebase:
- In
models.pyforeign keys: usesettings.AUTH_USER_MODEL - In view code: use
get_user_model()
3. Django Permissions Framework
Django models automatically generate four default permissions upon creation:
- 1
add_<modelname>(e.g.articles.add_article) - 2
change_<modelname>(e.g.articles.change_article) - 3
delete_<modelname>(e.g.articles.delete_article) - 4
view_<modelname>(e.g.articles.view_article)
Checking Permissions Programmatically
Guarding Views with @permission_required
Practice Quiz
Groups and Roles
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Django Admin Site | Groups and Roles |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.