Django 模型中的密码字段

2024-03-06

我正在尝试创建一个可以存储其他应用程序的用户名和密码的模型。如何在 Django 中设置密码字段,使其在管理中不是纯文本?提前致谢。


作为@mlissner建议 https://stackoverflow.com/questions/3715103/password-field-in-django-model/3715243#3715243 the auth.User模型是一个观察的好地方。如果您检查源代码 http://code.djangoproject.com/svn/django/trunk/django/contrib/auth/models.py你会看到password字段是一个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模型还有一个set_password方法。

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)

您可以从此方法中获取有关创建密码和保存密码的一些线索。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 模型中的密码字段 的相关文章

随机推荐