Django 模型中的密码字段 [英] Password field in Django model
本文介绍了Django 模型中的密码字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个模型,我可以在其中存储其他应用程序的用户名和密码.如何在 Django 中设置密码字段,使其在 admin 中不是纯文本?提前致谢.
I'm trying to create a model where I can store usernames and passwords for other applications. How can I set a password field in Django so that it is not in plain text in admin? Thanks in advance.
推荐答案
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.
这篇关于Django 模型中的密码字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文