Django Admin site and password field

4,719 views
Skip to first unread message

Gabe

unread,
May 11, 2011, 2:06:23 PM5/11/11
to Django users
Hi all,

this is my first post on this group so be gentle ;)

I`ve crated a model class and an admin site. My model Users has
password field. How can I rewrite it to be a password field and to get
stars when I`am typing password (when creating a new user by admin
site)?

Sorry for my English but it is not my native language.

Thx for any suggestions in advance.

Gabe

Shawn Milochik

unread,
May 11, 2011, 2:26:58 PM5/11/11
to django...@googlegroups.com
It sounds like you're not using the built-in Django admin, which would
make your life easier.

Assuming you want to do it the hard way, you can just use a
PasswordInput widget.
http://docs.djangoproject.com/en/1.3/ref/forms/widgets/


Gabe

unread,
May 12, 2011, 12:58:14 AM5/12/11
to Django users
I am using built-in Django admin. In my models.py file password field
is defined:

password = models.CharField(max_length=64)

I just can`t see password type field for models.

Oleg Lomaka

unread,
May 12, 2011, 4:20:58 AM5/12/11
to django...@googlegroups.com
Sorry, it's unclear you use django's auth.User model or you have custom model for your users?

If you have your custom model, then show an admin.py file from your app directory.

Jirka Vejrazka

unread,
May 12, 2011, 7:04:14 AM5/12/11
to django...@googlegroups.com
I have not done it myself, but you might need to specify custom widget
for your field. Take a look at PasswordInput widget here:
http://docs.djangoproject.com/en/1.3/ref/forms/widgets/

HTH

Jirka

Michal Petrucha

unread,
May 12, 2011, 7:52:34 AM5/12/11
to django...@googlegroups.com
On Wed, May 11, 2011 at 09:58:14PM -0700, Gabe wrote:
> I am using built-in Django admin. In my models.py file password field
> is defined:
>
> password = models.CharField(max_length=64)
>
> I just can`t see password type field for models.

It is hard for us to guess what you're actually trying to achieve.
Here are my guesses:

1) You created your own model which has nothing to do with the admin
infrastructure (i. e. the auth infrastructure used by the admin is
the default one bundled with Django). This model includes a
CharField that you want to represent in the admin using a
PasswordWidget.

In this case you'll just want to override the widget for this
specific field. (I don't know the specifics, you can look it up in
the tutorial or reference.)

2) You created a model replacing the default User model bundled in the
auth framework and are having issues with the admin login screen
using a regular TextInput for the password. This would be possible
if you specified a custom login_form to the AdminSite.

In this case you'll also want to override the widget used for this
form field to a PasswordInput.

I don't know if I guessed right but I hope it was at least a little
bit useful.

Michal Petrucha

signature.asc

Gabe

unread,
May 12, 2011, 12:51:24 PM5/12/11
to Django users
First of all thx for all replies.

For now I don`t use auth.User model I would like to create my own. So
I have written model class Users:

class Users(models.Model):
login =
models.CharField(max_length=64,error_messages={'required':'Podanie
loginu jest obowiązkowe','unique':'Podany login już istnieje. Wybierz
inny.'},help_text='Wybierz login do pracy w systemie.',unique=True)
password =
models.CharField(max_length=64,error_messages={'min_length':'Hasło
musi zawierać od 8 do 64 znaków','required':'Podanie hasła jest
wymagane'},help_text='Wprowadź hasło.')
email = models.EmailField(help_text='Podaj adres e-mail. Będzie on
używany do kontaktu z Tobą przez
administratora.',error_messages={'required':'Podanie e-maila jest
wymagane','invalid':'Format adresu jest niepoprawny'})
isActive = models.BooleanField(default=False)
priviledge = models.OneToOneField(Priviledges,blank=True)
activation_code = models.CharField(max_length=64,blank=True)

def __unicode__(self):
return self.login

Then I have registered it in admin panel by editing admin.py among
with another models:

from django.contrib import admin
from dgcmsUser.models import Users
from dgcmsUser.models import Priviledges
from dgcmsUser.models import UsersProfiles

admin.site.register(Users)
admin.site.register(UsersProfiles)
admin.site.register(Priviledges)


Then when I add user using autogenerated admin panel the password
field is <input type="text" /> not <input type="password" /> which I
would like it to be.

I haven`t done any other file editions.


Gabe
>  signature.asc
> < 1KWyświetlPobierz

Michal Petrucha

unread,
May 12, 2011, 5:00:29 PM5/12/11
to django...@googlegroups.com
On Thu, May 12, 2011 at 09:51:24AM -0700, Gabe wrote:
> First of all thx for all replies.
>
> For now I don`t use auth.User model I would like to create my own. So
> I have written model class Users:
>
> [...]

>
> Then I have registered it in admin panel by editing admin.py among
> with another models:
>
> from django.contrib import admin
> from dgcmsUser.models import Users
> from dgcmsUser.models import Priviledges
> from dgcmsUser.models import UsersProfiles
>
> admin.site.register(Users)
> admin.site.register(UsersProfiles)
> admin.site.register(Priviledges)
>
>
> Then when I add user using autogenerated admin panel the password
> field is <input type="text" /> not <input type="password" /> which I
> would like it to be.

You have to create a custom ModelForm for your Users model. This form
will only have to override the widget of the password field. Then
you'll have to create a custom ModelAdmin specifying your ModelForm
instead of the default one.

It may look something like this (note that I'm typing off the top of
my head):

class UserForm(forms.ModelForm):
class Meta:
widgets = {
'password': PasswordInput,
}

class UserAdmin(admin.ModelAdmin):
form = UserForm

admin.site.register(User, UserAdmin)


OT: Note that each time someone saves a plaintext password in a
database, a kitten dies. Please, think of the kittens. (-; (Also
applies to base64-encoded passwords and such. Yeah, you wouldn't
believe it but the academic information system our university forces
us to use does exactly that. Probably because of unicode encoding
issues.)

Michal

signature.asc

Gabe

unread,
May 13, 2011, 5:38:22 AM5/13/11
to Django users
Thanks for all suggestion. Subject to close (if it was forum ;))
>  signature.asc
> < 1KWyświetlPobierz
Reply all
Reply to author
Forward
0 new messages