Why Updating object not triggering at first time?

I have two function. My first function updating an Boolean filed True to False. My first function working properly but my second function not updating object False to True. If I try two time then second time my second function updating object False to True. I am not understanding why it’s not updating at first time?

here is my code:

function1 #this function working properly

views.py

def ForgetPassword(request):
    if request.method == "POST":
    .....my others code
   profile_obj = UserProfile.objects.get(user=user_obj)
      
   profile_obj.email_confirmed = False
   profile_obj.save() 
   .....my others code

function2 #this function not working properly If I try two time then second time my this function updating object False to True:

class ChangePassword_link(View):
        #my others code....

        if user is not None and account_activation_token.check_token(user, token):
            profile_obj = UserProfile.objects.filter(user=user).update(email_confirmed = True)
            messages.success(request, ('Your account have been confirmed. Now you can login'))
            return redirect('members:change-password-page')
                
        else:
            messages.warning(request, ('The confirmation link was invalid, possibly because it has already been used.'))
            return redirect('members:change-password-page') 
        
       #my others code....

The default value is false for email_confirmed fields in my model.

When this happened to me, it was because of a custom “save” method in my model.

From the docs here: QuerySet API reference | Django documentation | Django
update() does an update at the SQL level and, thus, does not call any save() methods on your models.`

So, you may need to add profile_obj.save() if that’s the case.

same problem still now after trying your recommended method:

profile_obj = UserProfile.objects.get(user=user)
profile_obj.email_confirmed = True
profile_obj.save()

save still now not triggering first time time.

Can you post your model?

here is my user profile model:

class UserProfile(models.Model):

      user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,related_name="userprofile")

      
      email_confirmed = models.BooleanField(default=False)

      forget_password_token = models.CharField(max_length=100,blank=True,null=True)

Any idea ??? why save method not calling at first time?

With no custom save methods, your original view should work.

profile_obj = UserProfile.objects.filter(user=user).update(email_confirmed = True)

Just try and debug the ‘user’ variable. Change it to (where user with pk 1 has an unconfirmed email).

profile_obj = UserProfile.objects.filter(pk=1).update(email_confirmed = True)

If this works, then its not the model query.

now object isn’t updating after using profile_obj = UserProfile.objects.filter(pk=16).update(email_confirmed = True) previously object was updating after tried two times but now object completely not updating even multiple try