Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom User admin breaks change password form in Django's admin

I'm using a custom User admin by:

class CustomUserAdmin(admin.ModelAdmin):
    model = User
    ...
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

but when I try to change the password via the admin page I get a 404.

user object with primary key u'4/password' does not exist.

Reverting back to the default User admin works fine.

like image 624
john2x Avatar asked Nov 13 '11 04:11

john2x


People also ask

What is the Django command to create a user or password for the admin user interface?

To create a new admin user in Django, we use the createsuperuser command, and this command can be executed using the manage.py utility. Once we execute the given command, it will prompt some fields like Username, Email, Password, Confirm Password.

What is the default username and password for Django admin?

Username: ola Email address: [email protected] Password: Password (again): Superuser created successfully. Return to your browser.


2 Answers

The default UserAdmin in django.contrib.auth.admin implements a lot of functionality, including the change password page.

Your CustomUserAdmin should subclass UserAdmin instead of admin.ModelAdmin, unless you want to reimplement that functionality yourself.

class CustomUserAdmin(UserAdmin):
    # as an example, this custom user admin orders users by email address
    ordering = ('email',)

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
like image 82
Alasdair Avatar answered Oct 14 '22 00:10

Alasdair


Also:

As per the docs, if you inherit from AbstractBaseUser you cannot use the default UserAdmin; or, put another way, you can but only a subset of the functionality will work - changing an existing password might work, but adding a new user will throw exceptions.

like image 39
Patrick Robertson Avatar answered Oct 13 '22 23:10

Patrick Robertson