Django模型中的密码字段 [英] Password field in Django model
本文介绍了Django模型中的密码字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个模型,我可以在其中存储其他应用程序的用户名和密码。如何在Django中设置密码字段,以便管理员不会显示纯文本?感谢提前。
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
模型是一个很好的地方。如果您检查源代码,看到密码
字段是一个 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>."))
用户
模型也有一个 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屋!
查看全文