straykiwi
straykiwi

Reputation: 546

Django custom user admin change_password

I'm successfully using a custom user model with django. The last thing to get working is the "AdminChangePasswordForm" for superusers to change any users password.

currently the change password link from admin:myapp:user gives a 404

The answer.

Override get_urls

and override UserChangeForm to have the correct url.

Upvotes: 5

Views: 2108

Answers (2)

rivf
rivf

Reputation: 163

So I had similar problem. When I tried to change user password from admin I got url to "/admin/accounts/siteuser/password/" (siteuser is the name of my custom user model) and 404 error with this message: "user object with primary key u'password' does not exist." The investigation showed that the problem was due to bug in django-authtools (1.4.0) as I used NamedUserAdmin class to inherit from.

So the solution is either (if you need to inherit from any custom UserAdmin like NamedUserAdmin from django-authtools):

from django.contrib.auth.forms import UserChangeForm
from authtools.admin import NamedUserAdmin
class SiteUserAdmin(NamedUserAdmin):
    ...
    form = UserChangeForm
    ...

or just inherit from default django UserAdmin:

from django.contrib.auth.admin import UserAdmin
class SiteUserAdmin(UserAdmin):
    pass

Upvotes: 3

JimmyYe
JimmyYe

Reputation: 844

It seems it's a "bug" in 1.7.x, and fixed in 1.8.x, which set the url name, so you do have to override get_urls():

from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
from django.conf.urls import url


class UserAdmin(AuthUserAdmin):
    def get_urls(self):
        return [
            url(r'^(.+)/password/$', self.admin_site.admin_view(self.user_change_password), name='auth_user_password_change'),
        ] + super(UserAdmin, self).get_urls()

URL:

password_change_url = urlresolvers.reverse('admin:auth_user_password_change', args=(1,))

Upvotes: 2

Related Questions