我想将我的 settings.AUTH_USER_MODEL
添加到我的 User
模型的管理中。
我用 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 = ()
在字段集中 Personal info
我添加了所有自定义信息。现在我还想显示所有继承的字段,如 first_name
、username
等。我可以将它们一个一个添加到字段集中,但我不确定这样是否正确.
有没有办法只从 User
模型继承它们而不明确指定?
最佳答案
您可以使用 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
关于python - 如何使用自定义用户从 Django 管理中的用户继承字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696498/