0

I am using Django with a MYSQL backend and I have noticed that there is a delay between when I update the data in my database and when the new data is available. Here is what's going on in my code:

def change_user_email_api(request):
logger.debug("API: resend verification email")
new_email = request.GET.get('email', '')
username = request.user.username
the_user = User.objects.get(username=username)
the_user.email = new_email
the_user.save()
send_verification_email(request.user)
return generate_success(callback)   

send_verification_email sends out an email to the user to verify their email account. I'm thinking the error is due to me passing request.user instead of the_user to send_verification_email.

Is there any way that I an ensure that I always have the newest data from the db, and if so, should I be doing this anyway? Or should I just accept that the data will take some time to update and design around this?

Thanks!

2
  • 1
    sounds like something else is going wrong there. updating properties on an instance are visible directly, even without the save(). please provide more context, or a real example. (yours looks made up, e.g. you are missing the string quotes) Commented Nov 26, 2012 at 23:01
  • I have updated the question, and I think I found the answer but some verification would be nice. Thanks for your reply :) Commented Nov 26, 2012 at 23:23

1 Answer 1

2

request.user does not fetches you the User object from the database. Its the middleware which sets the object in the request scope. So, you can not expect when you use request.user to give you the updated instance of the user. If you want to send the email you will have to use 'the_user' object

def change_user_email_api(request):
   logger.debug("API: resend verification email")
   new_email = request.GET.get('email', '')
   the_user = request.user
   the_user.email = new_email
   the_user.save()
   send_verification_email(the_user)
   return generate_success(callback)   
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I thought. Thanks for the confirmation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.