对自定义Django用户模型的任何更改在Admin中都被完全忽略



我通过扩展AbstractUser而不是AbstractBaseUser来定制用户模型,因为我不需要删除用户名,我只需要通过电子邮件对用户进行身份验证,而且我仍然想使用Django附带的身份验证系统。因此,我只是将电子邮件地址定义为用户名,并在任何迁移之前扩展了AbstractUser。

但是管理员没有意识到这一点,并且完全忽略了我在Admin.py中指定的内容,除了register指令。以下是我的管理员.py 的内容

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import User as CustomUser
from .forms import UserChangeForm, UserCreationForm
# from django.utils.translation import ugettext_lazy as _
# I obviously tried by extending UserAdmin too, no result
class CustomUserAdmin(admin.ModelAdmin):
add_form = UserCreationForm
form = UserChangeForm
model = CustomUser
fields = ('email')
# commenting or uncommenting the following doesn't change anything
"""
list_display = ('email', 'is_staff', 'is_active',)
list_filter = ('email', 'is_staff', 'is_active',)
exclude = ('first_name',)
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',)
"""

admin.site.register(CustomUser, BaseUserAdmin)

我什么都试过了,但都没用。我无法在"添加用户"表单中添加电子邮件字段,也无法从中删除first和last_namehttp://127.0.0.1:8000/admin/users/user/

Django内置的Admin似乎忽略了在任何扩展AbstractUser 的类中所做的任何更改

显然,设置中的一切都是正确的。py:

INSTALLED_APPS = [
'users',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
# this is necessary for the custom user model and must to be taken into account before any migration will occur. 
AUTH_USER_MODEL = 'users.User'

models.py:

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _

class CustomUserManager(BaseUserManager):
"""
Custom user model manager where email is the unique identifier for 
authentication instead of username.
Between the username field (email) and password, you must put all
the required fields. extra_fields must contain all the optional 
fields.
"""
def create_user(self, email, username, password, **extra_fields):
"""
Create and save a User with the given email and password.
"""
if not email:
raise ValueError(_('The Email must be set'))
if not username:
raise ValueError("Users must have an Username")
user = self.model(
email = self.normalize_email(email),
username=username, 
)
user.set_password(password)
user.save()
return user

def create_superuser(self, email, username, password):
user = self.create_user(
email=email,
username=username,
password=password
)
user.is_admin = True
user.is_staff=True
user.is_superuser=True
user.save(using=self._db)
return user
"""
This model behaves identically to the default user model, but you’ll be able to customize it in the future if the need arises. This is the recommended behavior
"""
class User(AbstractUser):
email = models.EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
"""
The REQUIRED_FIELDS = ['username'] must be present, otherwise the following error will arise:
TypeError: create_superuser() missing 1 required positional argument: 'username'
"""
REQUIRED_FIELDS = ['username']
objects = CustomUserManager()
def getEmailField(self):
return self.email
def __str__(self):
return self.email

forms.py:

class UserChangeForm(auth_forms.UserChangeForm):
class Meta(auth_forms.UserChangeForm.Meta):
model = CustomUser
fields = '__all__'

class UserCreationForm(auth_forms.UserCreationForm):
class Meta(auth_forms.UserCreationForm.Meta):
model = CustomUser
fields = '__all__'

很明显,我在fields='whatst'中尝试了一些东西,但我尝试的任何和每一个更改都被愉快地忽略了,管理员将始终以与默认相同的方式显示数据和表单。

我的观点.py…………

from django.shortcuts import render, redirect
from django.urls import reverse
from django.contrib.auth import login
from users.forms import CustomUserCreationForm
from django.template import RequestContext
def dashboard(request):
return render(request, "users/dashboard.html")
def register(request):
if request.method == "GET":
return render(
request, "users/register.html",
{"form": CustomUserCreationForm}
)
elif request.method == "POST":
form = CustomUserCreationForm(request.POST)

if form.is_valid():
# the form.cleaned_data must be used after calling 
# the form.is_valid method. 
# if(email_matches(form.cleaned_data["email"], form.cleaned_data["email2"])):
user = form.save()
login(request, user)
return redirect(reverse("dashboard"))

context = {
"form": form,
}
return render(request, "users/register.html", context)
def home_page(request):
return render(request, "home_page.html")

我在某个地方读到,不可能覆盖AbstractUser类中的任何内容,为此,我必须直接扩展AbstractBaseUser类。

这对管理员表格也有效吗?毕竟,我并没有覆盖任何内容,我只是试图在Admin Add User表单中显示电子邮件地址

但假设我不能真正自定义来自AbstractUser的任何内容。为什么会这样?这不是今天的问题。一个多星期以来,我一直在努力理解这种行为,无论是文件还是互联网都没有任何帮助。

您似乎使用了错误的管理员:

admin.site.register(CustomUser, CustomUserAdmin)

与其将admin.ModelAdmin扩展为CustomUserAdmin,不如执行以下操作-

class CustomUserAdmin(BaseUserAdmin):
...
...

然后请像这样更正您的型号注册行-

admin.site.register(CustomUser, CustomUserAdmin)

第页。S.别忘了取消对你所评论的行的注释。

最新更新