如何继承UserAdmin的所有字段以进行查看

时间:2015-11-15 04:21:45

标签: django django-admin

如何从UserAdmin继承所有现有的Fieldsets,然后从我的CustomUser添加其他字段?我可以添加自定义字段,但似乎无法获取所有现有的UserAdmin Fieldset以重新生成默认管理视图。 " UserAdmin.fieldsets + ...."的建议来自另一篇文章似乎没有用。

class CustomUserAdmin(UserAdmin):

    list_display = ('email', 'first_name', 'middle_name', 'last_name', 'mobile', 'date_of_birth', 'SSN', 'Primary_address', 'Primary_address_zipcode', 'is_admin','last_login','date_joined')
    list_filter = ('is_admin',)
    fieldsets = UserAdmin.fieldsets + (
            (None, {'fields': ('date_of_birth',)}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2')}
        ),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

1 个答案:

答案 0 :(得分:0)

您需要覆盖ModelAdmin的get_fieldsets方法:

class CustomUserAdmin(UserAdmin):

    def get_fieldsets(self, request, obj=None):
        fs = super(UserAdmin, self).get_fieldsets(request, obj)
        all_fields = fs[0][1]['fields']
        all_fields += ('email', 'password1', 'password2')
        return all_fields