views:

99

answers:

1

I have a Django site in which the site admin inputs their Twitter Username/Password in order to use the Twitter API. The Model is set up like this:

class TwitterUser(models.Model):
screen_name = models.CharField(max_length=100)
password = models.CharField(max_length=255)
def __unicode__(self):
    return self.screen_name

I need the Admin site to display the password field as a password input, but can't seem to figure out how to do it. I have tried using a ModelAdmin class, a ModelAdmin with a ModelForm, but can't seem to figure out how to make django display that form as a password input...

+6  A: 

From the docs, you can build your own form, something like this:

from django.forms import ModelForm, PasswordInput

class TwitterUserForm(ModelForm):
    class Meta:
        model = TwitterUser
        widgets = {
            'password': PasswordInput(),
        }

Or you can do it like this:

from django.forms import ModelForm, PasswordInput

class TwitterUserForm(ModelForm):
    password = forms.CharField(widget=PasswordInput())
    class Meta:
        model = TwitterUser

I've no idea which one is better - I slightly prefer the first one, since it means you'll still get any help_text and verbose_name from your model.

Regardless of which of those two approaches you take, you can then make the admin use your form like this (in your app's admin.py):

from django.contrib import admin

class TwitterUserAdmin(admin.ModelAdmin):
    form = TwitterUserForm

admin.site.register(TwitterUser, TwitterUserAdmin)
Dominic Rodger
I also would say go for the first one, I am not sure if the clean method for password will be the same in the second version.
KillianDS
Thanks, I did see this in the docs but forgot to add the `, TwitterUserAdmin` to the `admin.site.register` line, so it obviously wasn't working right. Thanks!
W_P
@Paul - no problem, glad I could help!
Dominic Rodger