Skip to content

Instantly share code, notes, and snippets.

@0xdeepmehta
Last active November 11, 2020 14:44
Show Gist options
  • Save 0xdeepmehta/b7d563edbc610c6dab39c850b65e045c to your computer and use it in GitHub Desktop.
Save 0xdeepmehta/b7d563edbc610c6dab39c850b65e045c to your computer and use it in GitHub Desktop.

Custom User Model in Django

There are two modern ways to create user model in django

  • AbstractUserBase : Use this option if you want to start from scratch by creating your own, completely new User model. It's more complicated .
  • AbstractUser : Use this option if you are happy with the existing fields on the User model and just want to remove the username/certain field. In fact this is inherited from AbstractUserBase.

Steps Invlove:

  • Create a Custom user model and manager.
  • update settings.py
  • create new UserCreation and userChangeForm
  • Update admin.py with customUser model

managers.py

from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _ 

class CustomUserManager(BaseUserManager):
  """
  Custom user model manager where email is the unique identifiers
  for authentication instead of usernames.
  """

  def create_user(self, email, password, **extra_fields):
      """
      Create and save a user with the given email and password.
      """

      if not email:
          raise ValueError(_,('The Email Must be set'))
      email = self.normalize_email(email)
      user = self.model(email=email, **extra_fields)
      user.set_password(password)
      user.save()
      return user


  def create_superuser(self, email, password, **extra_fields):
      """
      Create and save a SuperUser with the given email and password.
      """

      extra_fields.setdefault('is_staff', True)
      extra_fields.setdefault('is_superuser', True)
      extra_fields.setdefault('is_active', True)

      if extra_fields.get('is_staff') is not True:
          raise ValueError(_,('Superuser must have is_staff=True.'))
      if extra_fields.get('is_superuser') is not True:
          raise ValueError (_,('Superuser must have is_superuser=True'))
      return self.create_user(email, password, **extra_fields)

      

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser, AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.utils.translation import ugettext_lazy as _ 
from django.utils import timezone
from .managers import CustomUserManager


## Abstract User

class CustomUser(AbstractUser):
  username = None
  email = models.EmailField(_('Email Address'), unique=True)

  USERNAME_FIELD = 'email' # Set the USERNAME_FIELD -- which defines the unique identifier for the User model -- to email
  REQUIRED_FIELDS = []

  objects = CustomUserManager()

  def __str__(self):
      return self.email


## Abstract Base User

class CustomUser(AbstractBaseUser, PermissionsMixin):
  email = models.EmailField(_('Email address'), unique=True)
  is_staff = models.BooleanField(default=False)
  is_active = models.BooleanField(default=True)
  date_joined = models.DateTimeField(default=timezone.now)

  USERNAME_FIELD = 'email'
  REQUIRED_FIELDS = []

  objects = CustomUserManager()

  def __str__(self):
      return self.email

settings.py

AUTH_USER_MODEL = 'users.CustomUser'

forms.py

from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from .models import CustomUser


class CustomUserCreationForm(UserCreationForm):

  class Meta(UserCreationForm):
      model = CustomUser
      fields = ('email',)


class CustomUserChangeForm(UserChangeForm):

  class Meta:
      model = CustomUser
      fields = ('email',)

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser


class CustomUserAdmin(UserAdmin):
  add_form = CustomUserCreationForm
  form = CustomUserChangeForm
  model = CustomUser
  list_display = ('email', 'is_staff', 'is_active',)
  list_filter = ('email', 'is_staff', 'is_active',)
  fieldsets = (
      (None, {'fields': ('email', 'password')}),
      ('Permissions', {'fields': ('is_staff', 'is_active')}),
  )
  add_fieldsets = (
      (None, {
          'classes': ('wide',),
          'fields': ('email', 'password1', 'password2', 'is_staff', 'is_active')}
      ),
  )
  search_fields = ('email',)
  ordering = ('email',)


admin.site.register(CustomUser, CustomUserAdmin)

tests.py

from django.test import TestCase
from django.contrib.auth import get_user_model

class UsersManagersTest(TestCase):
   def test_create_user(self):
       User = get_user_model()
       user = User.objects.create_user(email="[email protected]", password="testpass")
       self.assertEqual(user.email, "[email protected]")
       self.assertTrue(user.is_active)
       self.assertFalse(user.is_staff)
       self.assertFalse(user.is_superuser)

       try:

           # userName is None for AbstractUse option
           # username does not exist for the AbstractUserBase options

           self.assertIsNone(user.username)
       except AttributeError:
           pass
       with self.assertRaises(TypeError):
           User.objects.create_user()
       with self.assertRaises(TypeError):
           User.objects.create_user(email='')
       with self.assertRaises(ValueError):
           User.objects.create_user(email='', password="foo")

   def test_create_superuser(self):
       User = get_user_model()
       admin_user = User.objects.create_superuser('[email protected]', 'foo')
       self.assertEqual(admin_user.email, '[email protected]')
       self.assertTrue(admin_user.is_active)
       self.assertTrue(admin_user.is_staff)
       self.assertTrue(admin_user.is_superuser)
       try:
           # username is None for the AbstractUser option
           # username does not exist for the AbstractBaseUser option
           self.assertIsNone(admin_user.username)
       except AttributeError:
           pass
       with self.assertRaises(ValueError):
           User.objects.create_superuser(
               email='[email protected]', password='foo', is_superuser=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment