姜戈。 models.py 中的哈希密码

Django. Hash password in models.py

我的数据库中有一个触发器,用于从另一列创建密码并在当前数据库中更新它。然后我将这个密码插入另一个table。 问题是我的项目不需要注册表作为新页面。所以只有管理员可以添加新用户。 这个问题听起来像:是否有机会在模型中创建哈希密码?或者从另一方面如何创建密码填写 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}'

触发于 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。有 users_student table 和 auth_user table 我一起工作。

P.P.S How my passwords look like

非常感谢

Django 有自己的 auth 框架,我建议你要么从 AbstractUser or you can look at the open source code of the User model and read documentation on password hashes 继承,然后写一些类似的东西。