gpt4 book ai didi

python - manage.py createsuperuser 不适用于自定义用户模型

转载 作者:行者123 更新时间:2023-11-30 22:11:17 24 4
gpt4 key购买 nike

我正在使用 Django,并且我创建了一个自定义用户模型,以便能够使用电子邮件作为主要登录方法而不是用户名。一切正常,但我无法使用命令创建 super 用户 python manage.py createsuperuser.

这是我的代码:

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)

这里是ma​​nagers.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)

该进程创建一个普通用户而不是 super 用户,并且它被创建。但我无法登录管理面板 /admin

最佳答案

您可以像下面这样实现 UserManager。它比您的代码更简单。

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

关于python - manage.py createsuperuser 不适用于自定义用户模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51462947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com