VicenteC
VicenteC

Reputation: 422

Setting value of certain attribute of a model related to User

I have a model related to User (relationship: OneToOne), in this model I have a field named email_confirmation. I can access to this field but I can't updated it.

models.py

class Profile(models.Model):
    profile_user               =   models.OneToOneField(User, ...) 
    profile_email_confirmation =   models.BooleanField(default=False)

views.py

def mail_confirmation(request, uidb64, token):
    uid = force_text(urlsafe_base64_decode(uidb64))
    user = User.objects.get(pk=uid)
     ...
    if user is not None and account_activation_token.check_token(user, token):
        user.profile.profile_email_confirmation = True
        user.save() #This isn't working and doesn't cause any error
        login(request, user) #This is working  
        return redirect('/home') #This is working

This function isn't causing any error so I don't know what is wrong I actually get redirect to /home (logged). I also can access to the field profile_email_confirmation

When I check the model in the Admin page, the profile_email_confirmation field has not been altered.

Upvotes: 0

Views: 18

Answers (1)

JPG
JPG

Reputation: 88569

You need to save the profile instance as well

if user is not None and account_activation_token.check_token(user, token):
    user.profile.profile_email_confirmation = True
    user.profile.save() # add this extra line
    user.save()
    login(request, user)
    return redirect('/home')

Upvotes: 2

Related Questions