赞
踩
every blog every motto: There’s only one corner of the universe you can be sure of improving, and that’s your own self.
https://blog.csdn.net/weixin_39190382?spm=1010.2135.3001.5343
django中使用AbstractUser 对用户进行扩展小结,汇总了若干种方法
class UserInfo(AbstractUser):
phone= models.CharField(max_length=12, verbose_name='phone')
like= models.CharField(max_length=200, verbose_name='like')
class Meta:
db_table = 'userinfo'
verbose_name = '用户'
verbose_name_plural = '用户管理'
# 指定使用自定义的模型类:
AUTH_USER_MODEL = 'bapp.UserInfo'
迁移模型到数据库
$ python manage.py makemigrations
$ python manage.py migrate
需要在admin.py里面注册一下,否则,用户组不会在页面上显示,如下图:
在admin.py文件中
admin.site.register(UserInfo,UserAdmin)
出现了我们的“用户管理”,如下图
但是,点进用户的详细信息,并没有显示我们添加的字段
使用如下方式,
@admin.register(UserInfo)
class ProfileAdmin(admin.ModelAdmin):
list_display = ['username', 'phone', 'like']
如图所示,显示了我们添加的字段
但存在一个小问题,添加用户时,username不在第一行,
逻辑也很简单,就是新填两个额外的字段,
ADDITIONAL_FIELDS = ((None, {'fields': ('phone', 'like')}),)
class MyUserAdmin(UserAdmin):
fieldsets = UserAdmin.fieldsets + ADDITIONAL_FIELDS
add_fieldsets = UserAdmin.fieldsets + ADDITIONAL_FIELDS
admin.site.register(UserInfo, MyUserAdmin)
但是,笔者并未实现成功,新填用户保存时,报如下错误,而且并未指定哪里出错,尝试多次,未果
和上一种逻辑一样的,添加新的字段
fields = list(UserAdmin.fieldsets)
fields[0] = (None, {'fields': ('username', 'password', 'phone')})
UserAdmin.fieldsets = tuple(fields)
admin.site.register(UserInfo, UserAdmin)
在用户详情中会出现,我们添加的字段phone,但是用户页面并没有出现,如下所示
代码如下
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy
class UserProfileAdmin(UserAdmin):
list_display = ('username', 'last_login', 'is_superuser', 'is_staff', 'is_active', 'date_joined')
fieldsets = (
(None, {'fields': ('username', 'password', 'first_name', 'last_name', 'email')}),
(gettext_lazy('User Information'), {'fields': ('phone','like')}),
(gettext_lazy('Permissions'), {'fields': ('is_superuser', 'is_staff', 'is_active',
'groups', 'user_permissions')}),
(gettext_lazy('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
admin.site.register(UserInfo, UserProfileAdmin)
用户页显示的信息如下
用户页的详细信息,如下图所示
细心的读者可能发现了,用户的详细信息中的几个“板块”,分别对应上面代码中fieldsets 的若干个元素!!!
读者可选择合适自己的即可
# -------------------------------------------------
# 方法一
@admin.register(UserInfo)
class ProfileAdmin(admin.ModelAdmin):
list_display = ['username', 'phone', 'like']
# --------------------------------------------------
# 方法二
ADDITIONAL_FIELDS = ((None, {'fields': ('phone', 'like')}),)
class MyUserAdmin(UserAdmin):
fieldsets = UserAdmin.fieldsets + ADDITIONAL_FIELDS
add_fieldsets = UserAdmin.fieldsets + ADDITIONAL_FIELDS
admin.site.register(UserInfo, MyUserAdmin)
# -----------------------------------------------------
# 方法三
fields = list(UserAdmin.fieldsets)
fields[0] = (None, {'fields': ('username', 'password', 'phone')})
UserAdmin.fieldsets = tuple(fields)
admin.site.register(UserInfo, UserAdmin)
# -------------------------------------------------------
# 方法四
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy
class UserProfileAdmin(UserAdmin):
list_display = ('username', 'last_login', 'is_superuser', 'is_staff', 'is_active', 'date_joined')
fieldsets = (
(None, {'fields': ('username', 'password', 'first_name', 'last_name', 'email')}),
(gettext_lazy('User Information'), {'fields': ('phone','like')}),
(gettext_lazy('Permissions'), {'fields': ('is_superuser', 'is_staff', 'is_active',
'groups', 'user_permissions')}),
(gettext_lazy('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
admin.site.register(UserInfo, UserProfileAdmin)
[1] https://blog.csdn.net/qq_40655579/article/details/83903004
[2] https://www.thinbug.com/q/48011275
[3] https://zhuanlan.zhihu.com/p/141696501
[4] https://blog.csdn.net/qq_41954715/article/details/109574939
[5] https://blog.csdn.net/a54288447/article/details/126649938
[6] https://blog.csdn.net/a54288447/article/details/126649938
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。