简体   繁体   中英

How to change password of user in AbstractBaseUser

In creation of MyUser I was following:

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". 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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