我尝试创建User
django 中的 s ( django.contrib.auth.models.User
) 通过两种不同的方式:
- 使用
User.objects.create(username="<username_here>", password="<password_here>", email="<email_here>)
-> 当我使用它时,登录用户以及随之而来的所有其他好处auth.models.User
没有收到,例如我无法使用authenticate
以这种方式创建的用户。 - 使用
User.objects.create_user("<username>", "<email>", "<password>")
。以这种方式做事没有任何问题,您可以使用 djangoauth
附带的所有免费内容。应用程序。
我的问题是,为什么会这样?
最佳答案
看看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/