Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password field in Django model

I'm trying to create a model where I can store usernames and passwords for other applications. How can I set a password field in Django so that it is not in plain text in admin? Thanks in advance.

like image 933
Ruben Quinones Avatar asked Sep 15 '10 06:09

Ruben Quinones


People also ask

Is there password field in Django models?

The Django's Forms The above form has two inputs - a text field named username (the name attribute in the html input field is what determines the name of input field) and a password field named password - and a submit button. The form uses POST method to submit form data to server.

How are passwords stored in Django?

Django provides a flexible password storage system and uses PBKDF2 by default. Those are the components used for storing a User's password, separated by the dollar-sign character and consist of: the hashing algorithm, the number of algorithm iterations (work factor), the random salt, and the resulting password hash.

How do I find my Django username and password?

Try using the set_password(raw_password) method to give the user a new password. Remember to call the save() method to ensure you save the change to the database.


1 Answers

As @mlissner suggested the auth.User model is a good place to look. If you check the source code you'll see that the password field is a CharField.

password = models.CharField(_('password'), max_length=128, help_text=_("Use  '[algo]$[salt]$[hexdigest]' or use the <a href=\"password/\">change password form</a>.")) 

The User model also has a set_password method.

def set_password(self, raw_password):     import random     algo = 'sha1'     salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]     hsh = get_hexdigest(algo, salt, raw_password)     self.password = '%s$%s$%s' % (algo, salt, hsh) 

You can take some clues from this method about creating the password and saving it.

like image 78
Manoj Govindan Avatar answered Sep 19 '22 00:09

Manoj Govindan