session modification question

22 views
Skip to first unread message

Spork Spork

unread,
Jan 18, 2014, 12:11:56 PM1/18/14
to django...@googlegroups.com
Hi,

I have a question about what gets persisted when session data gets updated. I've read the sessions chapter of the book, and it's not entirely clear to me.

Say I have two keys in the session object that I'm manipulating in a view:

view1: request.session['foo'] = 'foocontent'
view1: request.session['bar'] = 'barcontent'
view1: goes off and does something that takes a long, long timer

Meanwhile some other view fires off and changes foo:

view2: request.session['foo'] = 'updateddfoocontent'

Then view2 exits, and the most recent session data gets persisted.

But meanwhile view1 still has the old copy of the session data. Eventually it completes whatever it was doing, and updates bar:

view1: request.session['bar'] = 'updatedbarcontent'

Question: when view1 exits and its data gets persisted, does it overwrite all of the session data, including stomping on view2's modification of foo? If so, how can I ensure that at the end of view1, what gets persisted is:

session['foo'] = 'updatedfoocontent'
session['bar'] = 'updatedbarcontent'

I'm using django 1.4 and db-based sessions.

Thanks in advance,

Spork

Daniel Roseman

unread,
Jan 18, 2014, 2:24:40 PM1/18/14
to django...@googlegroups.com
The session is not suitable for this sort of partial modification. It is serialized and unserialized as a whole, so there is no way to prevent the data being "stomped".

You should store the data somewhere else: probably in a model.
--
DR. 

ernando

unread,
Jan 20, 2014, 4:04:53 AM1/20/14
to django...@googlegroups.com
Hi,

I agree with Daniel that your case of using session is incorrect. You have to think about session as a simple dictionary that stores data through requests. And it doesn't keep track about protecting data from changing - you should think about it yourself. Believe, you have to think about some kind of flag, that should be checked while running time-consuming operation and handle cases of changed data.

Thanks,
Dmitry
Reply all
Reply to author
Forward
0 new messages