pythondjangosqlitedjango-modelsdjango-modeladmin

Django. Hash password in models.py


I have a trigger in my database to create a password from another column and update it in the current database. Then I insert this password to another table. The problem is my project doesn't need a registration form as a new page. So only admin can add new users. The question sounds like: is there an opportunity to create a hash password in models? Or from another hand how to create password fill in models.py?

models.py

class Student(models.Model):
    studentIndex = models.IntegerField()
    studentName = models.CharField(max_length=50)
    studentSurname = models.CharField(max_length=50, default='')
    studentPhoneNumber = models.CharField(max_length=12, default='+48')
    studentPesel = models.CharField(max_length=11, default='')
    studentStudyMode = models.CharField(max_length=12, default='')
    studentFaculty = models.CharField(max_length=30, default='')
    studentSemester = models.IntegerField(default='')
    studentAddress = models.CharField(max_length=255, default='')
    studentMotherName = models.CharField(max_length=100, default='')
    studentBirth = models.DateField(help_text='year-month-day')
    studentEmail = models.EmailField(max_length=255, default='')
    studentImage = models.ImageField(default='default.jpg', upload_to='profile_pics')
    studentPassword = models.CharField(max_length=100, default='12345678',
                                       help_text='Do not modify this field. Password will be generated automatically')
    studentDateJoined = models.DateTimeField(default=datetime.datetime.now())

    def __str__(self):
        return f'{self.studentIndex} - {self.studentName} {self.studentSurname}'

Trigger in user_student table

CREATE TRIGGER add_new_student_with_correct_password
    after insert
    on users_student
begin
    update users_student
    set studentPassword = strftime('%Y', studentBirth)
        || substr(strftime('%m', studentBirth), 2, 1)
        || strftime('%d', studentBirth)
        || substr(studentMotherName, 1, 1)
        || lower(substr(studentName, 1, 1))
    where studentPassword = '12345678';

    insert into auth_user(password, last_login, is_superuser, username, first_name, email, is_staff, is_active,
                          date_joined, last_name)
    values (strftime('%Y', studentBirth)
                || substr(strftime('%m', studentBirth), 2, 1)
                || strftime('%d', studentBirth)
                || substr(studentMotherName, 1, 1)
                || lower(substr(studentName, 1, 1)),
            null,
            false,
            new.studentIndex,
            new.studentName,
            new.studentEmail,
            false,
            true,
            new.studentDateJoined,
            new.studentSurname);
end;

P.S. There are users_student table and auth_user table that I work with.

P.P.S How my passwords look like

Thx a lot


Solution

  • Django has its own auth framework and I suggest you either inherit from AbstractUser or you can look at the open source code of the User model and read documentation on password hashes and then write something similar.