django1.5 subclass AbstractUser, How to get a password-(re)set field in admin

1,042 views
Skip to first unread message

Michael Muster

unread,
Nov 3, 2012, 11:15:21 AM11/3/12
to django...@googlegroups.com
Hi again,

I have a subclass from AbstractUser

1 from django.contrib.auth.models import AbstractUser
2 from django.conf import settings
3
4 class cpUser(AbstractUser):
5 twitter = models.CharField(max_length=100)
6 def __unicode__(self):
7 return self.username

and
AUTH_USER_MODEL = 'account.cpUser'
in my settings.py set.

How do i get a password field to set and reset
a password in my admin app?
Adding the password field to admin.py does obviously not
work as it enters plain text and not the hashed password.

1 from django.contrib import admin
2 class cpUserAdmin(admin.ModelAdmin):
3 fields = ['twitter','username', 'first_name', 'last_name', 'password',]
4 ~~~~~~~~
5 admin.site.register(cpUser, cpUserAdmin)


Do i have to set a passwort field to the models.py or
can i geht that from the models which i "abstracted" from
(as done with username, first_name, last_name...)



Best regards
Michael




signature.asc

Russell Keith-Magee

unread,
Nov 3, 2012, 7:07:35 PM11/3/12
to django...@googlegroups.com
You need to follow the instructions that are in the documentation.


The key is that you can't just subclass admin.ModelAdmin -- you need to subclass the existing Django admin class for Users (django.contrib.auth.admin.UserAdmin) - that base class is what provides all the special password handling etc for User models.

Yours,
Russ Magee %-) 

Detectedstealth

unread,
Nov 30, 2012, 1:07:32 AM11/30/12
to django...@googlegroups.com
Hi Russel,


password = ReadOnlyPasswordHashField(label=_("Password"),

        help_text=_("Raw passwords are not stored, so there is no way to see "

                    "this user's password, but you can change the password "

                    "using <a href=\"password/\">this form</a>."), widget=ReadOnlyPasswordHashWidget()) 

From what I seen in django/contrib/auth/forms UserChangeForm this gives me the change password URL however I get the error: 'CustUser' object has no attribute 'username' looking at the error line 136

/Library/Python/2.7/site-packages/django/contrib/auth/admin.py in user_change_password

  1.             'title': _('Change password: %s') % escape(user.username), 
It looks like this is either a bug or doesn't work with a custom user as planed as it looks for a username even though in the example you set the email to username field with: USERNAME_FIELD = 'email'

Am I missing something?

Regards,
Bruce

Russell Keith-Magee

unread,
Dec 1, 2012, 12:32:02 AM12/1/12
to django...@googlegroups.com
Hi Bruce,

Update your Django checkout. This problem was fixed in 0fc5878c.

Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/sRmpO1m1Ng0J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Bruce Wade

unread,
Dec 1, 2012, 2:29:59 AM12/1/12
to django...@googlegroups.com
Hi Russ, 

Thanks that did solve the problem I was having. 

--
Regards,
Bruce
Reply all
Reply to author
Forward
0 new messages