AttributeError: 'NoneType' object has no attribute 'is_superuser'

I'm working with Django, and I keep getting this error when trying to create a new superuser inside the terminal:

AttributeError: 'NoneType' object has no attribute 'is_superuser'

I've tried everything I've looked up to fix it. No luck.

Also, this code is greyed-out inside my user model, and when I hover over the text it says: Code is unreachable Pylance

if not email:
            raise ValueError('Users must have an email address')

            email = self.normalize_email(email)
            email = email.lower()

            user = self.model(
                email = email,
                name = name
            )

            user.set_password(password)
            user.save(using=self._db)

            return user

Here is my full user model:

from abc import abstractmethod
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager

class UserAccountManager(BaseUserManager):
    @abstractmethod
    def create_user(self, email, name, password=None):
        if not email:
            raise ValueError('Users must have an email address')

            email = self.normalize_email(email)
            email = email.lower()

            user = self.model(
                email = email,
                name = name
            )

            user.set_password(password)
            user.save(using=self._db)

            return user

    def create_realtor(self, email, name, password=None):
        user = self.create_user(email, name, password)

        user.is_realtor = True
        user.save(using=self._db)

        return user

    def create_superuser(self, email, name, password=None):
        user = self.create_user(email, name, password)

        user.is_superuser = True
        user.is_staff = True

        user.save(using=self._db)

        return user


class UserAccount(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False) # set to false for Auth

    is_realtor = models.BooleanField(default=False)

    objects = UserAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']

    def __str__(self):
        return self.email

You need to create a common create_user method which can accept extra_fields:

def create_user(self, email, name, password=None, **extra_fields):
    if not email:
        raise ValueError('Users must have an email address')
    email = self.normalize_email(email)
    email = email.lower()
    user = self.model(
            email = email,
            name = name,
           **extra_fields
        )

    user.set_password(password)
    user.save(using=self._db)
    return user

Now, when you will call create_superuser method, call your create_user method like this:

def create_superuser(self, email, name, password=None):
    return self.create_user(email, name, password, is_superuser=True, is_staff=True)
Back to Top