尝试在 Django 用户模型中将标识符切换为电子邮件时出错
我想将 Django 用户模型的唯一标识符从用户名更改为电子邮件,所以我这样写:
models.py:
from django.db import models
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUserManager(BaseUserManager):
'''
Custom user model manager where email is the unique identifier for authentication instead of username.'
'''
def create_user(self, email, password, **extra_fields):
'''
Create and save a User with the given email and password.
'''
if not email:
raise ValueError('The Email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
'''
Create and save a SuperUser with the given email and password.
'''
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=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)
class CustomUser(AbstractUser):
username = None
email = models.EmailField('email address', unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
# Register your models here.
class CustomUserAdmin(UserAdmin):
model = CustomUser
list_display = ('email', 'is_staff', 'is_active')
list_filter = ('email', 'is_staff', 'is_active')
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Permissions', {'fields': ('is_staff', 'is_active')}),
)
add_fieldsets = (
(
None,
{
'classes': ('wide',),
'fields': ('email', 'password1', 'password2', 'is_staff', 'is_active'),
},
),
)
search_fields = ('email',)
ordering = ('email',)
settings.py:
AUTH_USER_MODEL = 'users.CustomUser'
python manage.py makemigrations
和 python manage.py migrate
命令均已成功完成。
当我运行 python manage.py createsuperuser 时,它会要求提供电子邮件(这是正确的),然后是密码,但之后我得到以下信息:
AttributeError: 'CustomUserManager' object has no attribute 'create_superuser'. Did you mean: 'create_user'?
我迷路了。
我的代码有什么问题? 如果还有其他方法可以做到这一点,请给我教程链接吗?
I want to change the unique identifier of Django's User model from Username to Email so I write this:
models.py:
from django.db import models
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUserManager(BaseUserManager):
'''
Custom user model manager where email is the unique identifier for authentication instead of username.'
'''
def create_user(self, email, password, **extra_fields):
'''
Create and save a User with the given email and password.
'''
if not email:
raise ValueError('The Email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
'''
Create and save a SuperUser with the given email and password.
'''
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=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)
class CustomUser(AbstractUser):
username = None
email = models.EmailField('email address', unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
# Register your models here.
class CustomUserAdmin(UserAdmin):
model = CustomUser
list_display = ('email', 'is_staff', 'is_active')
list_filter = ('email', 'is_staff', 'is_active')
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Permissions', {'fields': ('is_staff', 'is_active')}),
)
add_fieldsets = (
(
None,
{
'classes': ('wide',),
'fields': ('email', 'password1', 'password2', 'is_staff', 'is_active'),
},
),
)
search_fields = ('email',)
ordering = ('email',)
settings.py:
AUTH_USER_MODEL = 'users.CustomUser'
Both python manage.py makemigrations
and python manage.py migrate
commands completed successfully.
When I run python manage.py createsuperuser
it asks for Email
(which is correct) and then passwords but I get the following after that:
AttributeError: 'CustomUserManager' object has no attribute 'create_superuser'. Did you mean: 'create_user'?
I'm lost.
What is the issue with my code?
If there's any other way to do that, would you please give me the link to tutorial?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论