我在models.py文件中有一个AbstractUser类,以扩展django用户默认类,
class Profile(AbstractUser):
address = models.CharField('Address', max_length=255, null=True)
在我的admin.py中,
class ProfileAdmin(UserAdmin):
pass
一切正常,但当我尝试添加个人资料字段时,地址字段不会显示在管理员中:
class ProfileAdmin(UserAdmin):
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email','address')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
我将'address'字段添加到fieldset变量,但是当我转到我的个人资料管理页面时,“地址”字段不会出现。无论如何都要将我的Profile类中的字段添加到我的个人资料管理页面?
提前致谢,
答案 0 :(得分:3)
我想这是因为您忘记覆盖UserAdmin的属性add_fieldsets
,更好的是in the auth docs:
class UserAdmin(BaseUserAdmin):
....
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal info', {'fields': ('date_of_birth',)}),
('Permissions', {'fields': ('is_admin',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'date_of_birth', 'password1', 'password2')}
),
)
只需覆盖add_fieldsets
即可。