我创建了一个扩展AbstractBaseUser类的自定义用户模型。代码如下。
class UserModel(AbstractBaseUser):
sys_id = models.AutoField(primary_key=True, blank=True)
name = models.CharField(max_length=127, null=False, blank=False)
email = models.EmailField(max_length=127, unique=True, null=False, blank=False)
mobile = models.CharField(max_length=10, unique=True, null=False, blank=False)
user_type = models.PositiveSmallIntegerField(choices=user_type_choices, null=False, blank=True, help_text="Admin(1)/Institute(2)/Student(3)")
access_valid_start = models.DateTimeField(null=True, blank=True)
access_valid_end = models.DateTimeField(null=True, blank=True)
created_when = models.DateTimeField(null=True, blank=True )
created_by = models.BigIntegerField(null=True, blank=True)
last_updated_when = models.DateTimeField(null=True, blank=True)
last_updated_by = models.BigIntegerField(null=True, blank=True)
notes = models.CharField(max_length=2048, null=True, blank=True)
is_active = models.BooleanField(default=True)
# this field is required to login super user from admin panel
is_staff = models.BooleanField(default=True)
# this field is required to login super user from admin panel
is_superuser = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = "email"
# REQUIRED_FIELDS must contain all required fields on your User model,
# but should not contain the USERNAME_FIELD or password as these fields will always be prompted for.
REQUIRED_FIELDS = ['name', 'mobile', 'user_type']
class Meta:
app_label = "accounts"
db_table = "users"
def __str__(self):
return self.email
def get_full_name(self):
return self.name
def get_short_name(self):
return self.name
# this methods are require to login super user from admin panel
def has_perm(self, perm, obj=None):
return self.is_superuser
# this methods are require to login super user from admin panel
def has_module_perms(self, app_label):
return self.is_superuser
现在只在我的管理员面板'群组'是可用的。我希望我的自定义用户模式,即UserModel出现在管理面板中,以便我可以从管理员用户界面添加,编辑和删除用户。
我尝试了来自SO的多个代码,其中最简单的是
from django.contrib.auth.admin import UserAdmin
from accounts.models import UserModel
from django.contrib import admin
class MyUserAdmin(UserAdmin):
model = UserModel
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('mobile',)}),
)
admin.site.register(UserModel, MyUserAdmin)
现在我收到了这个错误..
错误:
<class 'accounts.admin.MyUserAdmin'>: (admin.E019) The value of 'filter_horizontal[0]' refers to 'groups', which is not an attribute of 'accounts.UserModel'.
<class 'accounts.admin.MyUserAdmin'>: (admin.E019) The value of 'filter_horizontal[1]' refers to 'user_permissions', which is not an attribute of 'accounts.UserModel'.
<class 'accounts.admin.MyUserAdmin'>: (admin.E033) The value of 'ordering[0]' refers to 'username', which is not an attribute of 'accounts.UserModel'.
<class 'accounts.admin.MyUserAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'username', which is not a callable, an attribute of 'MyUserAdmin', or an attribute or method on 'accounts.UserModel'.
<class 'accounts.admin.MyUserAdmin'>: (admin.E108) The value of 'list_display[2]' refers to 'first_name', which is not a callable, an attribute of 'MyUserAdmin', or an attribute or method on 'accounts.UserModel'.
<class 'accounts.admin.MyUserAdmin'>: (admin.E108) The value of 'list_display[3]' refers to 'last_name', which is not a callable, an attribute of 'MyUserAdmin', or an attribute or method on 'accounts.UserModel'.
<class 'accounts.admin.MyUserAdmin'>: (admin.E116) The value of 'list_filter[3]' refers to 'groups', which does not refer to a Field.
请告诉我如何成功向管理界面添加自定义用户模型?
答案 0 :(得分:4)
如果您的自定义用户模型扩展了django.contrib.auth.models.AbstractUser,则可以使用Django现有的django.contrib.auth.admin.UserAdmin类。 但是,如果您的用户模型扩展了AbstractBaseUser,则需要定义自定义的ModelAdmin类。可以继承默认的django.contrib.auth.admin.UserAdmin; 但是,您需要覆盖任何引用django.contrib.auth.models.AbstractUser上不在您的自定义用户类上的字段的定义。
基本上,如果您从AbstractBaseUser
而不是AbstractUser
创建新的用户模型,则需要创建自己的自定义ModelAdmin
类,或者如果您继承自UserAdmin
(这是你当前正在做的事情)你需要覆盖和处理任何差异。
答案 1 :(得分:1)
您正在尝试使用UserAdmin,但您没有引用的字段
您可能需要深入检查UserAdmin类,并查看应覆盖哪些变量
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2'),
}),
)
form = UserChangeForm
add_form = UserCreationForm
change_password_form = AdminPasswordChangeForm
list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
search_fields = ('username', 'first_name', 'last_name', 'email')
ordering = ('username',)
filter_horizontal = ('groups', 'user_permissions',)
此外,错误可以为您提供出现问题的提示
答案 2 :(得分:1)
@Anurag Rana,您的自定义UserAdmin
类应如下所示
from django.contrib.auth.admin import UserAdmin
from accounts.models import UserModel
from django.contrib import admin
class MyUserAdmin(UserAdmin):
model = UserModel
list_display = () # Contain only fields in your `custom-user-model`
list_filter = () # Contain only fields in your `custom-user-model` intended for filtering. Do not include `groups`since you do not have it
search_fields = () # Contain only fields in your `custom-user-model` intended for searching
ordering = () # Contain only fields in your `custom-user-model` intended to ordering
filter_horizontal = () # Leave it empty. You have neither `groups` or `user_permissions`
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('mobile',)}),
)
admin.site.register(UserModel, MyUserAdmin)