python - 使用两种不同的方式创建 django 用户

标签 python django django-models django-authentication

我尝试创建User django 中的 s ( django.contrib.auth.models.User ) 通过两种不同的方式:

  1. 使用User.objects.create(username="<username_here>", password="<password_here>", email="<email_here>) -> 当我使用它时,登录用户以及随之而来的所有其他好处 auth.models.User没有收到,例如我无法使用authenticate以这种方式创建的用户。
  2. 使用User.objects.create_user("<username>", "<email>", "<password>") 。以这种方式做事没有任何问题,您可以使用 django auth 附带的所有免费内容。应用程序。

我的问题是,为什么会这样?

最佳答案

看看create_user方法,你会发现它使用set_password方法来设置密码。

def create_user(self, username, email=None, password=None):
    """
    Creates and saves a User with the given username, email and password.
    """
    now = timezone.now()
    if not username:
        raise ValueError('The given username must be set')
    email = UserManager.normalize_email(email)
    user = self.model(username=username, email=email,
                      is_staff=False, is_active=True, is_superuser=False,
                      last_login=now, date_joined=now)

    user.set_password(password)
    user.save(using=self._db)
    return user

关于python - 使用两种不同的方式创建 django 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18698989/

相关文章:

python - 慢 scipy 双正交积分

django - 在 django View 之间共享 redis 连接

django - Django admin:对用户具有外键的ModelAdmin每行添加一个查询

django - 在 Django Admin 中编辑/显示主键

python - DJango 休息框架中的员工休假类型条件

python - 如何在Python中使用pandas在现有Excel工作表中追加列

python - 合并连续列中相同变量的级别

python - QSpacerItem 实例化两次时使 pyQT 崩溃

javascript - 添加 javascript 和 jquery 的 Django 模板

django - 上传文件保存后更新 FileField 文件名(Django)