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!
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