Django admin form for creating AbstractUser extended model

后端 未结 1 380
走了就别回头了
走了就别回头了 2021-01-06 04:27

I\'ve got a custome user model that extends/inherits AbstractUser. I also want the user creation form in admin to match, but for some reason I can only get it to show Usern

相关标签:
1条回答
  • 2021-01-06 05:10

    UserAdmin from django.contrib.auth.admin also sets the "add_fieldsets" attribute, that sets the fields to be shown on the add user view. Since UserAdmin sets this field you need to overwrite it to set your own fields.

    Here is an example:

    class CustomUserAdmin(UserAdmin):
    # ...code here...
    
        fieldsets = (
            (None, {'fields': ('email',)}),
            (_('Personal info'), {'fields': ('first_name', 'last_name')}),
            (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                           'groups', 'user_permissions')}),
            (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        )
        add_fieldsets = (
            (None, {
                'classes': ('wide',),
                'fields': ('email', 'first_name', 'last_name', 'password1',
                           'password2')}
             ),
        )
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题