Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:26:38+00:00 2026-05-26T00:26:38+00:00

i noticed a strange behaviour when interacting with session variables in Django. In one

  • 0

i noticed a strange behaviour when interacting with session variables in Django.
In one of my app i created a middleware.py file containing a function that copy a session variable that stores an object into a local one and then i change an attribute of the object from the local var. It happens that the changes i make to the local variable are applied also to the session variable. It seems that the local var is only a reference to the session. Is this behaviour correct?
Here’s the code:

class CloudMiddleware(object):
    user = request.session['user']
    user.email = 'myemail'

When i do

user = request.session['user']
email = user.email

The value of email is equal to ‘myemail’.
I always thought i had to save my object back in the session if i want to store it. Could someone explain me how it really works?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:26:38+00:00Added an answer on May 26, 2026 at 12:26 am

    This is nothing to do with sessions, but a simple consequence of how Python mutable objects work. When you do user = request.session['user'] you are getting a reference to the object, exactly as you would with any other mutable object stored in a standard dictionary. So yes, when you change one of its attributes, that change is referenced in any other reference you have to it.

    Note that for sessions, this change will only be persisted in the lifetime of the current request. That’s because Django can’t know the session object has changed, so won’t save it unless you specifically tell it to – see the documentation for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that my Designer.vb file of one of my forms has a lot
I have noticed a strange behaviour of a str_replace function. Can you tell me
I noticed a strange behaviour of File.Copy() in .NET 3.5SP1. I don't know if
I noticed a strange behaviour today: It seems that, in the following example, the
I've noticed a strange behaviour of the live() function in jQuery: <a href=# id=normal>normal</a>
I have noticed that there are strange requests to my website trying to find
I've noticed some strange behaviour in JS window.location.hash = ''; var hash = window.location.hash;
I have noticed a strange behaviour when intercepting the preUpdate row hook in Propel
I am creating a responsive website, and have just noticed a strange behaviour in
after adding adMob ads to my application, I have noticed some strange memory behaviour.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.