Session array/list append does not work

Posted: February 1st, 2010 | Author: Davo | Filed under: Django | Tags: , , , | 1 Comment »

I was trying to save and update some arrays inside the Django session object but it was not working at all…

The following snippet DOES NOT WORK

request.session['array'] = [] # let's create an empty array
# try to append some integers...
request.session['array'].append(1)
request.session['array'].append(2)
request.session['array'].append(3)
...

In order to solve this we need to store the array in a temporary object first.
Quick fix:

request.session['array'] = []
tmp = request.session['array']
tmp.append(1)
tmp.append(2)
tmp.append(3)
request.session['array'] = tmp # this works now.
...