社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

Django admin,无法创建超级用户并使用自定义用户模型登录

ade desmond • 4 年前 • 621 次点击  

我用python manage.py create super user命令在django中创建了一个自定义用户模型,它创建了超级用户,但仍然将staff设置为False,superuser设置为False,admin设置为False当我试图使用用户登录到管理面板时,我得到的错误是我的凭据不正确。我已经尝试了所有的事情,并尽可能多地研究,但没有结果我需要帮助 这是我的密码

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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48534
 
621 次点击  
文章 [ 2 ]  |  最新文章 4 年前