gpt4 book ai didi

django - 如何在管理面板中添加自定义用户模型?

转载 作者:行者123 更新时间:2023-12-02 05:43:43 27 4
gpt4 key购买 nike

我创建了一个扩展 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.

请告诉我如何成功地将自定义用户模型添加到管理 UI 中?

最佳答案

来自Django Documentation

If your custom user model extends django.contrib.auth.models.AbstractUser, you can use Django’s existing django.contrib.auth.admin.UserAdmin class. However, if your user model extends AbstractBaseUser, you’ll need to define a custom ModelAdmin class. It may be possible to subclass the default django.contrib.auth.admin.UserAdmin; however, you’ll need to override any of the definitions that refer to fields on django.contrib.auth.models.AbstractUser that aren’t on your custom user class.

所以基本上,如果您从 AbstractBaseUser 而不是 AbstractUser 创建新的用户模型,您需要创建自己的自定义 ModelAdmin 类,或者,如果您继承自 UserAdmin (这就是您当前正在做的事情),您需要覆盖并处理任何差异。

关于django - 如何在管理面板中添加自定义用户模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43055664/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com