mateuszlaczynski
mateuszlaczynski

Reputation: 115

Fatal error while creating superuser in Django (django-rest-framework)

I want to make an AbstractUser without a username field so I got this:

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator

class CustomUser(AbstractUser):
    email = models.EmailField(max_length=254, unique=True)
    username = None

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    
    session_token = models.CharField(max_length=10, default=0)

    #Newsletter/Shipment Info (not required)
    newsletter = models.BooleanField(default=False)

    first_name = models.CharField(max_length=35, blank=True, null=True)
    last_name = models.CharField(max_length=35, blank=True, null=True)
    adress = models.CharField(max_length=50, blank=True, null=True)
    city = models.CharField(max_length=50, blank=True, null=True)
    postal_code = models.CharField(max_length=6, blank=True, null=True)
    phone = models.CharField(max_length=9, validators=[RegexValidator(r'^\d{9}$/')], blank=True, null=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

I got this migrated into the database and now I need to craete an admin user so I would be able to enter the admin panel. I typed email and both passwords into the comand prompt after python manage.py createsuperuser and I got this error:

Traceback (most recent call last):
  File "C:\Users\mateu\Documents\meadow\backend\manage.py", line 22, in <module>
    main()
  File "C:\Users\mateu\Documents\meadow\backend\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\__init__.py", line 425, in execute_from_command_line
    utility.execute()
  File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\base.py", line 373, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\base.py", line 417, in execute
    output = self.handle(*args, **options)
  File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 195, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() missing 1 required positional argument: 'username'

I have no idea how to fix it. Any thoughts?

Upvotes: 1

Views: 531

Answers (1)

Mohamed Hamza
Mohamed Hamza

Reputation: 985

You have to inherit from AbstractBaseUser not from AbstractUser and add Manager Class to it

from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin


class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    is_superuser = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    # Add other fields here

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []



class UserManager(BaseUserManager):
    
    def _create_user(self, email, password, **extra_fields):
        user = self.model(email=self.normalize_email(email), **extra_fields)
        user.set_password(password)        
        user.save(using=self._db)
        return user

    def create_user(self, email, password, **extra_fields):
        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)
        return self._create_user(email, password, **extra_fields)

Don't forget

python manage.py makemigrations

python manage.py migrate

I recommend you to take a look at the docs

Upvotes: 1

Related Questions