[Django] Append objects in request.session

This article is once again more of a reminder to me, i hope it will help everyone at the same time.

I was experiencing some issue using Django Session objects lately, i wanted to save a list in my session object and something strange happened.

The first creation of the list went just fine, but when i tried to append objects to this session everything seemed to look fine in the method where i was appending the data, but when from another method i tried to loop on this list, i only found the first item.

Let me show you some log to illustrate :

# first method before append - state of the session object :
[('saved', ['obj1']), ('_auth_user_backend', 'django.contrib.auth.backends.ModelBackend'), ('_auth_user_id', 1)]

# first method after append:
[('saved', ['obj1', 'obj2']), ('_auth_user_backend', 'django.contrib.auth.backends.ModelBackend'), ('_auth_user_id', 1)]

# second method :
[('saved', ['obj1']), ('_auth_user_backend', 'django.contrib.auth.backends.ModelBackend'), ('_auth_user_id', 1)]

Ok now this is strange… because it looks just like it worked, and then… no.

The answer came to me (as usual) googling the question and finding the answer on the DjangoFoo website. Therefore instead of doing something like that when appending a list in a session object :

	if not 'saved' in request.session or not request.session['saved']:
		request.session['saved'] = [obj]
	else:
		request.session['saved'].append(obj)

What you need to do is :

	if not 'saved' in request.session or not request.session['saved']:
		request.session['saved'] = [obj]
	else:
		saved_list = request.session['saved']
		saved_list.append(obj)
		request.session['saved'] = saved_list

It’s not very Pythonic or elegant, but that’s the way. This limitation comes from the following advice given out by Django’s documentation :

Use normal Python strings as dictionary keys on request.session. This is more of a convention than a hard-and-fast rule

Thank you for your time,
Vale

2 Commentaires

  1. There is a somewhat more pythonic way to do the job:

    if not request.session:
    request.session={}

    saved_list = request.session.get(‘saved’,[])
    saved_list.append(obj)
    request.session[‘saved’] = saved_list

  2. other methode

    add line « request.session.modified = True » for session update after your request

Laisser un commentaire