简体   繁体   English

如何在AbstractBaseUser中更改用户密码

[英]How to change password of user in AbstractBaseUser

In creation of MyUser I was following: 在创建MyUser时,我关注的是:

https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#a-full-example https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#a-full-example

I would like to change password of user. 我想更改用户密码。

I try: 我尝试:

In [55]: i=FairsUser.objects.get(email="admin@andilabs.com")

In [56]: i.__dict__
Out[56]:
{'_state': <django.db.models.base.ModelState at 0x110a53490>,
 'email': u'admin@andilabs.com',
 'id': 8,
 'is_active': True,
 'is_admin': False,
 'is_superuser': False,
 'last_login': datetime.datetime(2014, 4, 2, 16, 0, 59, 109673),
 'mail_sent': False,
 'password': u'pbkdf2_sha256$12000$XgszxXkXbroY$PEEf3vqszclGcf7iQXeZRWDTYcCsvlGh0jH15f6rKR8='}

In [57]: i.set_password("abcd")

In [58]: i.save()

Then I check: 然后我检查:

In [59]: i_updated=FairsUser.objects.get(email="admin@andilabs.com")

In [60]: i_updated.__dict__
Out[60]:
{'_state': <django.db.models.base.ModelState at 0x110a53590>,
 'email': u'admin@andilabs.com',
 'id': 8,
 'is_active': True,
 'is_admin': False,
 'is_superuser': False,
 'last_login': datetime.datetime(2014, 4, 2, 16, 0, 59, 109673),
 'mail_sent': False,
 'password': u'pbkdf2_sha256$12000$8VCDlzTuVfHF$ldwqbXo/axzMFLasqOKkddz8o1yW9d5r7gUxD3qH4sU='}

The values for hashed password differs, but I can not login using "abcd". 哈希密码的值不同,但是我无法使用“ abcd”登录。 What is the reason? 是什么原因?

OK. 好。 In this case it "started" working. 在这种情况下,它“开始”工作。 But In my logic in form of admin it does not still: 但是按照我的管理形式,它仍然没有:

def clean(self, commit=True):
    super(UserChangeForm, self).clean()
    cleaned_data = self.cleaned_data
    old_status = FairsUser.objects.get(email=cleaned_data['email'])
    if (old_status.mail_sent is False or old_status.mail_sent is None) and cleaned_data['mail_sent'] is True:
        usr = FairsUser.objects.get(email=cleaned_data['email'])
        new_pass = ''.join(
            random.choice(
                string.ascii_uppercase + string.digits
                ) for _ in range(DESIRED_PASSWORD_LENGTH))
        usr.set_password(new_pass)
        usr.save()
        mail_content = 'Hi! Bbelow are your login credentials.\n e-mail: {0} \n password: {1} \n Have nice time on fairs! \n'.format(usr.email, new_pass)
        msg = EmailMessage('Your login data', mail_content, 'from@andilabs.com', [usr.email, ])
        msg.send()
    return cleaned_data

The delivered by email password does not allow my to login, as well I can not authenticate in console. 通过电子邮件发送的密码不允许我登录,也无法在控制台中进行身份验证。

The reason WAS not connected with sending email, but with WRONG using of forms methods by me. 原因不是与发送电子邮件有关,而是由我错误地使用了表单方法。

This is the working solution in basic form (without sending email, and without generating new password in some random way) to make it clear for others, who end up here with the same problem of set_password while calling save() of the form in case of AbstractBaseUser 这是基本形式的有效解决方案(无需发送电子邮件,并且不会以某种随机方式生成新密码)以使其他人清楚,这些人最终在调用表单的save()时遇到相同的set_password问题,以防万一的AbstractBaseUser

def save(self, commit=True):
    user = super(UserChangeForm, self).save(commit=False)
    cleaned_data = self.cleaned_data
    old_status = FairsUser.objects.get(email=cleaned_data['email'])
    if (old_status.mail_sent is False or old_status.mail_sent is None) and cleaned_data['mail_sent'] is True:
        new_pass =  "SOME_NEW_VALUE_OF_PASSWORD"
        user.set_password(new_pass)
        # here sending email can be initiated
    if commit:
        user.save()
    return user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM