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

标签 python django django-users

我正在关注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/

相关文章:

python - 使用 gunicorn 核心转储时 GDB 为空回溯

python - 通过唯一标签合并 networkx 中的两个网络映射

django - 自定义 ModelResource 中的字段以下载数据

django - 使用 UserPassesTestMixin(基于类的 View )和重定向

python - Django UserProfile ...没有密码

Python:声明 2 字节变量

python - 重新安装后引导操作系统设置

python - 使用 Django REST 框架获取 token

python - 围绕 django 版本添加逻辑的最佳方法?

python - Django 表单无法识别模型属性