Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django custom user admin change_password

Tags:

python

django

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.

like image 685
straykiwi Avatar asked Sep 27 '22 11:09

straykiwi


2 Answers

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
like image 125
rivf Avatar answered Oct 13 '22 01:10

rivf


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,))
like image 36
JimmyYe Avatar answered Oct 13 '22 02:10

JimmyYe