6

I'm using Django, and I've created a custom user model to be able to use E-mail as main login method instead of username.. everything worked but I can't create a superuser using the command python manage.py createsuperuser.

Here are my codes :

models.py

from django.db import models
from django.core.mail import send_mail
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.db.models import CASCADE
from django.utils.translation import ugettext_lazy as _
from .managers import UserManager

    class User(AbstractBaseUser, PermissionsMixin):
        user_name = models.CharField(_('user name'), max_length=35)
        email = models.EmailField(_('email address'), unique=True)
        date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
        is_active = models.BooleanField(_('active'), default=True)
        is_staff = models.BooleanField(_('staff'), default=False)
        is_superuser = models.BooleanField(_('superuser'), default=False)
        avatar = models.ImageField(upload_to='media/', null=True, blank=True)

        objects = UserManager()


        USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = []


        class Meta:
            verbose_name = _('user')
            verbose_name_plural = _('users')

        def email_user(self, subject, message, from_email=None, **kwargs):
            '''
            Sends an email to this User.
            '''
            send_mail(subject, message, from_email, [self.email], **kwargs)

And here are managers.py

from django.contrib.auth.base_user import BaseUserManager

    class UserManager(BaseUserManager):
        use_in_migrations = True

        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)
            user.set_password(password)
            user.save(using=self._db)
            return user

        def create_user(self, email, password=None, **extra_fields):
            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_superuser', 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)

The process creates a normal user not superuser , and it is created. but I can't log in to admin panel /admin

Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116

3 Answers3

11

You can implement the UserManager like following. it's simpler than your code.

class UserManager(BaseUserManager):

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

        if email is None:
            raise TypeError('Users must have an email address.')

        user = self.model(email=self.normalize_email(email))
        user.set_password(password)
        user.save()

        return user

    def create_superuser(self, email, password):

        if password is None:
            raise TypeError('Superusers must have a password.')

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

        return user
Ali
  • 2,541
  • 2
  • 17
  • 31
1

Add this field to User model

is_admin = models.BooleanField(default=False)

and in UserManager set is_admin = True:

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

For your SuperUser, make sure that is_staff is true, so add these line to your User model

@property
def is_staff(self):
    return self.is_admin

Try out this and tell if it works

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50
0

I was having this problem too. When doing createsuperuser operation, I got this:

site-packages/django/db/migrations/loader.py", line 187, in check_key
return self.graph.root_nodes(key[0])[0]

I finally found my fix which is different from those listed here. My fix was: I needed to do a django migration of my custom user model. Then it works!

TaiwanGrapefruitTea
  • 1,045
  • 2
  • 14
  • 25