且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django 模型中的密码字段

更新时间:2023-11-04 15:52:28

As @mlissner 建议 auth.User 模型是一个很好的查看位置.如果您查看源代码看到 password 字段是一个 CharField.

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>."))

User 模型也有一个 set_password 方法.

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.