Django中User.objects.create_user()与User.objects.create()与User().save()之间的区别 [英] Difference between User.objects.create_user() vs User.objects.create() vs User().save() in django

查看:91
本文介绍了Django中User.objects.create_user()与User.objects.create()与User().save()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django文档中,它使用 User.objects.create_user()创建用户.我很困惑,那与 User.objects.create() User().save()之间有什么区别,它们是创建其他对象的常规方法型号

In the Django documentation, it uses User.objects.create_user() to create a user. I am confused, what is the difference between that and User.objects.create() and User().save() which are the normal ways to create objects of other models

推荐答案

最重要的区别是,如果您向 .create()方法提供密码,则会在用户,并且将无法用于验证用户.

The most significant difference is that if you supply a password to the .create() method, it will be set verbatim on the user, and will not be usable to authenticate the user.

>>> user = User.objects.create(username="foo", password="bar")
>>> user.password
'bar'

相反, create_user()方法对密码参数进行散列,然后是密码.

Instead, the create_user() method hashes the password argument, which is then.

>>> user = User.objects.create_user(username="foo", password="bar")
>>> user.password
'pbkdf2_sha256$120000$2pHBVu3tYYbK$ESG1nbUY2ZhEmstJ7Fzu1DioXmGYXiLw31YDkOGn9E0='

请参见 https://github.com/django/django/blob/master/django/contrib/auth/models.py#L145 .

还进行了其他一些整理".请参阅上面链接的其余功能.

Some other 'tidying' is also done. See the rest of the function linked above.

这篇关于Django中User.objects.create_user()与User.objects.create()与User().save()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆