reading-notes

Django Custom User

Readings

Videos

Notes

  1. Subclassing AbstractBaseUser is the recommended way to create a custom user model

     from django.contrib.auth.models import AbstractBaseUser
    
     class CustomUser(AbstractBaseUser):
         pass
    
  2. Create a custom manager for the custom user model by subclassing BaseUserManager

     from django.contrib.auth.models import BaseUserManager
    
     class CustomUserManager(BaseUserManager):
         pass
    
  3. Add fields to the custom user model, such as email, username and password

     from django.db import models
    
     class CustomUser(AbstractBaseUser):
         email = models.EmailField(unique=True)
         username = models.CharField(max_length=40, unique=True)
         password = models.CharField(max_length=100)
         full_name = models.CharField(max_length=100)
         phone_number = models.CharField(max_length=15)
    
  4. Add the custom manager to the custom user model

     class CustomUser(AbstractBaseUser):
         email = models.EmailField(unique=True)
         username = models.CharField(max_length=40, unique=True)
         password = models.CharField(max_length=100)
         full_name = models.CharField(max_length=100)
         phone_number = models.CharField(max_length=15)
         objects = CustomUserManager()
    
  5. Update the settings.py file to use the custom user model

     AUTH_USER_MODEL = 'your_app.CustomUser'
    
  6. Create migration for the custom user model

     python manage.py makemigrations
    
  7. Apply the migration

     python manage.py migrate