0
votes

I created a custom user model by name Shop into django and when i create the super user with this command python manage.py createsuperuser it creates the super user but still setting is_staff to False, is_superuser to False and also is_admin to false. when i try to login to the admin panel using the user i get the error that my credentials are not correct.I have tried everything and research as much as i can but to no avail. I need help this is my code

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, 
PermissionsMixin
from django.utils.text import slugify
from django.utils.crypto import get_random_string # generates a random string 
from django.db import IntegrityError






class UserManager(BaseUserManager):

def create_user(self, email, shop_name='', mall_name='', password=None, **kwargs):
    if not email:
        raise ValueError('You must enter an email address')

    user = self.model(email=self.normalize_email(email), shop_name=shop_name, mall_name=mall_name)
    user.set_password(password)
    user.save(using=self._db)
    return user 


def create_superuser(self,email, password):

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


list_of_malls = [('Dubai mall', 'Dubai mall'),
             ('Dubai Festival City Mall','Dubai Festival City Mall'),
             ('Ibn Battuta Mall', 'Ibn Battuta Mall')

            ]

class Shop(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, blank=False)
date_joined = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
shop_name = models.CharField(max_length=50, null=True)
mall_name = models.CharField(choices=list_of_malls, max_length=50, null=True)
slug = models.SlugField(max_length=50, unique=True) 
is_staff = models.BooleanField(default=False)


objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []

def save(self, *args, **kwargs):
    if not self.slug:
        try:
            self.slug = slugify(self.shop_name + " shop " + get_random_string(20) )
            super(Shop, self).save(*args, **kwargs)
            #add shop_name with 10 randomly generated characters to create slug

        except IntegrityError:
            pass 

def __str__(self):
    return self.email
2

2 Answers

0
votes

I think it's because you are using create_user in your create_superuser function, and in create_user you do not set is_staff or is_superuser to True, nor should you. Try this create_superuser function instead:

def create_superuser(self, email, password, **kwargs):
    user = self.model(
        email=email,
        is_staff=True,
        is_superuser=True,
        is_active=True,
        **kwargs
    )
    user.set_password(password)
    user.save(using=self._db)
    return user
0
votes

There's a couple of issues with the code you've posted.

First, you're not passing is_admin, is_superuser and is_staff to the user model when you create it in create_user(). Those arguments are captured in kwargs and should be passed such as this:

def create_user(self, email, shop_name='', mall_name='', password=None, **kwargs):
    if not email:
        raise ValueError('You must enter an email address')

    user = self.model(
        email=self.normalize_email(email), 
        shop_name=shop_name, 
        mall_name=mall_name,
        **kwargs  # Pass the additional keyword arguments 
    )
    ...

Second, in the save() method of Shop, you are only calling save() on the superclass when there is no slug. The call to super(Shop, self).save() should probably always happen - e.g.

def save(self, *args, **kwargs):
    try:
        if not self.slug:
            self.slug = slugify(self.shop_name + " shop " + get_random_string(20) )
        super(Shop, self).save(*args, **kwargs)  # Always call the save in the superclass
        #add shop_name with 10 randomly generated characters to create slug

    except IntegrityError:
        pass  # Bad idea to swallow exceptions.

It's also a bad idea to silently ignore the IntegrityError. Better to drop the try/except entirely, or at least log the error.