gpt4 book ai didi

python - django 如何知道 create_user() 函数中的密码是什么

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:05 26 4
gpt4 key购买 nike

我正在关注 django's doc关于创建自定义用户模型,我在定义 create_user()create_superuser() 时感到困惑。定义 create_user() 时,传递的密码参数是 None

def create_user(self, email, date_of_birth, password=None):
# create user here
...

创建 create_superuser() 时,密码由调用者提供。

def create_user(self, email, date_of_birth, password=None):
# create user here
...

django 如何知道 create_user() 函数中的密码?如果这是一个菜鸟问题,我很抱歉,但我真的需要理解。你能帮我理解这里的概念吗?谢谢。

最佳答案

create_user 方法定义中的password=None 意味着None 是密码的默认值,但它仍然可以由调用者提供。

当您不为新用户提供密码或者您为密码传递None时,Django 将创建具有不可用密码的用户,这意味着用户将无法使用用户名和密码登录.

它在您想要强制执行其他身份验证方式(如 OAuth 或 OpenID 等)的情况下很有用。

相关代码部分带注释:

为什么 create_superuser 需要密码?其实不然。您始终可以将 None 作为密码传递,但恕我直言,这是因为您需要 super 用户的特殊权利和特权,并且您不像普通用户那样经常创建 super 用户。

关于python - django 如何知道 create_user() 函数中的密码是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33554379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com