Skip to content

Instantly share code, notes, and snippets.

Embed
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(
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(
email,
password=password,
)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser, PermissionsMixin):
name = models.CharField(max_length=50, default='Anonymous')
email = models.EmailField(max_length=100, unique=True)
username = None
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
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