uda*_*kls 0 python authentication django
我检查了具有相同问题的其他问题,并尝试使用解决方案解决,但没有任何帮助。在终端中创建超级用户时,我既没有看到“名字”也没有看到“姓氏”输入,我一直在遵循本教程“ https://www.youtube.com/watch?v=HshbjK1vDtY&t=3422s ”,但是导师在做与我正在做的完全相同的事情时没有遇到任何问题,所以我无处可以找到解决方案
这是我的 models.py
from django.db import models
from django.contrib.auth.models import(
AbstractBaseUser, BaseUserManager
)
class UserManager(BaseUserManager):
def create_user(self, first_name, last_name, email, password=None, is_active=True, is_staff=False, is_admin=False):
if not first_name:
raise ValueError("Users must have an first name")
if not last_name:
raise ValueError("Users must have an last name")
if not email:
raise ValueError("Users must have an email address")
if not password:
raise ValueError("Users must have a password")
user = self.model(
email=self.normalize_email(email)
)
user.first_name = first_name
user.last_name = last_name
user.set_password(password)
user.staff = is_staff
user.admin = is_admin
user.active = is_active
user.save(using=self._db)
return user
def create_staffuser(self, email, first_name, last_name, password=None):
user = self.create_user(
email,
first_name,
last_name,
password=password,
is_staff=True
)
return user
def create_superuser(self, email, first_name, last_name, password=None):
user = self.create_user(
email,
first_name,
last_name,
password=password,
is_admin=True,
is_staff=True
)
return user
class User(AbstractBaseUser):
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=50, blank=True, null=True)
email = models.EmailField(max_length=254, unique=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELD = ['first_name', 'last_name']
objects = UserManager()
def __str__(self):
return self.email
def get_first_name(self):
return self.first_name
def get_last_name(self):
return self.last_name
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
@property
def is_active(self):
return self.active
Run Code Online (Sandbox Code Playgroud)
我已将其导入到 admin.py admin.py 中
from django.contrib import admin
from django.contrib.auth import get_user_model
from .forms import UserAdminChangeForm, UserAdminCreationForm
from .models import User
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
User = get_user_model()
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserAdminChangeForm
add_form = UserAdminCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ('email', 'admin')
list_filter = ('admin', 'staff', 'admin')
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal info', {'fields': ('first_name', 'last_name')}),
('Permissions', {'fields': ('admin', 'staff', 'active')}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'first_name', 'last_name', 'password1', 'password2')}
),
)
search_fields = ('email', 'first_name', 'last_name')
ordering = ('email',)
filter_horizontal = ()
admin.site.register(User, UserAdmin)
# Remove Group Model from admin. We're not using it.
admin.site.unregister(Group)
Run Code Online (Sandbox Code Playgroud)
REQUIRED_FIELD应该REQUIRED_FIELDS
| 归档时间: |
|
| 查看次数: |
2589 次 |
| 最近记录: |