Django — User Management
User Model
from django.contrib.auth.models import User
# Create user
user = User.objects.create_user('john', 'john@example.com', 'password')
# Get user
user = User.objects.get(username='john')
# Check password
user.check_password('password')
# Set password
user.set_password('newpassword')
user.save()
User Profile
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(max_length=500, blank=True)
avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
Permissions
# Check permissions
user.has_perm('blog.add_post')
user.has_perm('blog.change_post')
# Set permissions
user.user_permissions.add(permission)
# Groups
from django.contrib.auth.models import Group
group = Group.objects.get(name='Editors')
user.groups.add(group)
Custom User Model
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
email = models.EmailField(unique=True)
bio = models.TextField(max_length=500, blank=True)
Mini Practice
- Create and manage users
- Create a user profile
- Set up permissions
- Create a custom user model
Up Next
Continue with Sessions — managing user sessions.
Related Topics
Frequently Asked Questions about User Management
What is User Management in Django?
User Management is a fundamental concept in Django. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn User Management?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn User Management.
Why is User Management important in Django?
User Management is essential for Django development. Understanding this concept will help you write better code and solve real-world problems more effectively.