我有一个代码,通过使用自定义模型扩展AbstractBaseUser来扩展用户模型。我收到此错误消息
(错误::(admin.E108)的值 'list_display [2]'指的是'first_name',它不是可调用的,a 'userAdmin'的属性,或'towns.MyUser'上的属性或方法。 :(admin.E108)的值 'list_display [3]'指的是'last_name',它不是可调用的,a 'userAdmin'的属性,或'towns.MyUser'上的属性或方法。
模型文件:
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not email:
raise ValueError('email address is required')
user = self.model(
username = username,
email = self.normalize_email(email)
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password=None):
user = self.create_user(
username, email, password=password
)
user.is_admin = True
user.is_staff = True
user.save(using=self._db)
return user
class MyUser (AbstractBaseUser):
username = models.CharField(
max_length=255,
unique=True
)
email = models.EmailField(
max_length=255,
unique=True,
verbose_name='email address'
)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
表单文件:
from django.contrib.auth import get_user_model
from django.db.models import Q
User = get_user_model()
from django import forms
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='password', widget=forms.PasswordInput)
password2 = forms.CharField(label='password confirmation', widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email']
def clean_password(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError("passwords do not match")
return password2
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
class UserLoginForm(forms.Form):
query = forms.CharField(label='Username or Email')
password = forms.CharField(label='Password',widget=forms.PasswordInput)
def clean(self, *args, **kwargs):
query = self.cleaned_data.get('query')
password = self.cleaned_data.get('password')
user_qs_final = User.objects.filter(
Q(username__iexact=query) |
Q(email__iexact=query).distinct()
)
if not user_qs_final.exists() and user_qs_final.count != 1:
raise forms.ValidationError('Invalid credentials = user does not exist')
user_obj = user_qs_final.first()
if not user.obj.check_password(password):
raise forms.ValidationError(' password is not correct')
self.cleaned_data["user_obj"] = user_obj
return super(UserLoginForm, self).clean(*args, **kwargs)
管理员档案:
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .forms import UserCreationForm
from .models import MyUser
class UserAdmin(BaseUserAdmin):
add_form = UserCreationForm
List_display = ('username', 'email', 'is_admin')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('username', 'email','password')}),
('Permissions', {'fields': ('is_admin',)}),
)
search_fields = ('username', 'email')
ordering = ('username','email')
filter_horizontal = ()
admin.site.register(MyUser,UserAdmin)
admin.site.unregister(Group)
我也将设置文件中的AUTH_USER_MODEL更改为
AUTH_USER_MODEL = 'towns.MyUser'
任何线索如何解决此错误
注意:我真的不需要那些字段(first_name),(last_name)
答案 0 :(得分:3)
由于管理类中的拼写错误,因此您不会覆盖包含list_display
的默认first_name
选项。它应该是list_display
,而不是List_display
内的UserAdmin
:
class UserAdmin(BaseUserAdmin):
add_form = UserCreationForm
list_display = ('username', 'email', 'is_admin')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('username', 'email','password')}),
('Permissions', {'fields': ('is_admin',)}),
)
search_fields = ('username', 'email')
ordering = ('username','email')
filter_horizontal = ()
admin.site.register(MyUser,UserAdmin)