Home > front end >  Django: How to create a superuser?
Django: How to create a superuser?

Time:01-23

Good Day,

I am trying to create a custom user model with AbstractBaseUser.

models.py

from django.db import models
from django.utils import timezone
from django.utils.translation import gettext as _
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin

# Create your models here.


class CustomAccountManager(BaseUserManager):
    def create_user(self, email, username, password, **other):

        email = self.normalize_email(email)
        user = self.model(
            email=email,
            username=username,
            **other
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):

        user = self.create_user(
            email,
            password=password,
            username=username,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class NewUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email adress'), unique=True)
    username = models.CharField(max_length=100, unique=True)
    start_date = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    objects = CustomAccountManager()

    def __str__(self):
        return self.email

I used the documentation to create it. enter image description here

With the password 'admin' and email '[email protected]'. But it doesnt work as you see in the screenshot.

Do you know the issue?

Thank you very much! :-)

Edit: settings.py

INSTALLED_APPS = [
    'abstractuser',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

AUTH_USER_MODEL = 'abstractuser.NewUser'

CodePudding user response:

The is_staff field is what determines whether the user may login to the admin site. In your code, you're not setting this property to True when creating superusers.

You need to either set user.is_staff = True in your create_superuser function or replace the is_staff field with a property that reads from is_admin.

def create_superuser(self, email, username, password):

    user = self.create_user(
        email,
        password=password,
        username=username,
    )
    user.is_admin = True
    user.is_staff = True  # can access the admin site
    user.save(using=self._db)
    return user

CodePudding user response:

Solution 1:

class NewUser(AbstractBaseUser, PermissionsMixin):
     .
     .
     .
     @property
     def is_admin(self):
         if is_superuser and is_staff:
            return True
         return False

Solution 2:

def create_superuser(self, email, password, **other):
        other.setdefault('is_staff', True)
        other.setdefault('is_superuser', True)

        if other.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if other.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        return self._create_user(email, password, **other)
  •  Tags:  
  • Related