[Django]-Password Widget and Password Hashing in Django Admin

3👍

You must not do this.

Django includes an authentication framework that has everything you need already, including the ability to customise the model to include your date of birth field.

(Note, there is no point including age as well; you can always calculate that from the dob when you need it.)

2👍

A great place to see how this is done is to look at the Django source code in django.contrib.auth, they handle this same scenario for a User.

The idea is that you would use a custom admin form with a password field so that you can get the raw password from the form and then manually set the password running it through a hasher first.

from django.contrib.auth.hashers import make_password

class Student(models.Model):

    def set_password(self, raw_password):
        self.password = make_password(raw_password)

The make_password also takes a hasher type arguement if you don’t want to use the default one.

Leave a comment