python-如何使用自定义用户从Django admin中的User继承字段?
作者:互联网
我想将我的settings.AUTH_USER_MODEL添加到由用户模型安装的管理员中.
我用在文档中找到的snippet进行注册:
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserChangeForm
add_form = UserCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ('email', 'bdate')
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal info', {'fields': ('bdate', 'website', 'location')}),
)
# 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', 'password1', 'password2')}
),
)
search_fields = ('email',)
ordering = ('email',)
filter_horizontal = ()
在字段集“个人信息”中,我添加了所有自定义信息.现在,我还想显示所有继承的字段,例如first_name,username等.我可以将它们一个接一个地添加到字段集中,但是我不确定这是否正确.
有没有一种方法可以从用户模型继承它们而无需明确指定?
解决方法:
您可以使用ModelAdmin.get_fieldsets():
class UserAdmin(BaseUserAdmin):
def get_fieldsets(self, request, obj=None):
fieldsets = list(super(UserAdmin, self).get_fieldsets(request, obj))
# update the `fieldsets` with your specific fields
fieldsets.append(
('Personal info', {'fields': ('bdate', 'website', 'location')}))
return fieldsets
标签:django-admin,python,django 来源: https://codeday.me/bug/20191119/2034264.html