Skip to content

Instantly share code, notes, and snippets.

Created August 16, 2020 03:15
What would you like to do?
Custom User Model in Django
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
class UserManager(BaseUserManager):
def create_user(self, email, password=None):
Creates and saves a User with the given email and password.
if not email:
raise ValueError('Users must have an email address')
user = self.model(
return user
def create_superuser(self, email, password):
Creates and saves a superuser with the given email and password.
user = self.create_user(
user.is_staff = True
user.is_superuser = True
return user
class CustomUser(AbstractBaseUser, PermissionsMixin):
name = models.CharField(max_length=50, default='Anonymous')
email = models.EmailField(max_length=100, unique=True)
username = None
session_token = models.CharField(max_length=10, default=0)
active = models.BooleanField(default=True)
# a admin user; non super-user
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False) # a superuser
created_at = models.DateTimeField(
auto_now_add=True, blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True)
objects = UserManager()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment