Custom user model in Django and super user

I am using Django as backend for my mobile app. When creating users info I need the following info:name, DoB and firebaseUID. Please note that I do not want their email or password. But I want to create superusers who act as admins and want the superusers to be created using email and password. So I am not sure if I have to:

  1. Create a custom user by extending AbstractBaseUser and have a user manager attached to that custom user OR
  2. Create a custom user by extending AbstractBaseUser and then create another Django user class which is only for superusers.

I implemented option 1 with the below code, but I get the following error: "TypeError: MyUserManager.create_superuser() missing 1 required positional argument: 'email'" Also when I am creating superuser, Django is asking for name, firebaseUID and DOB fields although I want to create superusers only email and password fields.

from django.contrib.auth.models import AbstractBaseUser
from django.db import models
from django.utils import timezone

class MyUserManager(models.Manager):

    def _create_user(self, email, password, **extra_fields):
        Creates and saves a User with the given email and password.
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        return user

    def create_user(self, email=None, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', 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)
    def get_by_natural_key(self, email):
        return self.get(email=email)

class CustomUser(AbstractBaseUser):
    name = models.CharField(null=True, blank=True, max_length=100)
    userName = models.CharField(
        null=True, blank=True, max_length=100, unique=True)
    firebaseUid = models.CharField(
        null=True, blank=True, max_length=100, unique=True)
    dob = models.DateField(max_length=8)
    created_at = models.DateTimeField(
    email = models.EmailField(unique=True) #used only for createing super user at this point 

    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    # countryCode = models.CharField(null=True, blank=True, max_length=5)

    USERNAME_FIELD = 'userName'
    REQUIRED_FIELDS = ['name', 'firebaseUid', 'dob']

    objects = MyUserManager()

    def __str__(self):
        return self.userName

    def get_fBaseUid_name(self):
        return self.firebaseUid

    def has_perm(self, perm, obj=None):
        return self.is_superuser

    def has_module_perms(self, app_label):
        return self.is_superuser


Please let me know if you need any other info. Thank you in advance!

Has your User model been specified in your

If it hasn't, specify it

AUTH_USER_MODEL = "yourapp.UserModel"

Also, in your CustomUser model

Do this

# For creating a superuser using Django

And if you want to create users with "email" and "password", not "username" and "password"

Do this


You can use this code for your create_superuser as well

def create_superuser(self, email, password=None):

   if password is None:
      raise TypeError("Password should not be none")

   user = self.create_user(email, password)
   user.is_superuser = True
   user.is_staff = True
   return user

I hope that helps

Back to Top