[Answered ]-Django3: ManyToMany field causes: Unknown field(s) (username) specified for User. Check fields/fieldsets/exclude attributes of class CustomUserAdmin

1πŸ‘

βœ…

The username field is still part of the search_fields and the add_fieldsets. You thus should remove the username field there:

class CustomUserAdmin(UserAdmin):
    list_display = ('email', 'is_staff', 'is_superuser')
    readonly_fields = ('last_login', 'date_joined', 'uuid')
    ordering = ('email',)
    search_fields = ('first_name', 'last_name', 'email')  # πŸ–˜ no username
    fieldsets = (
        (
            'Fields',
            {
                'fields': (
                    'email',
                    'uuid',
                    'date_joined',
                    'last_login',
                    'is_active',
                    'is_staff',
                    'is_superuser',
                    'groups',
                    'user_permissions',
                    'password',
                )
            },
        ),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),
            #              πŸ–ž without username
        }),
    )


admin.site.register(User, CustomUserAdmin)

Leave a comment