How to lookup django session for a particular user? How to lookup django session for a particular user? django django

How to lookup django session for a particular user?


This answer is being posted five years after the original question, but this SO thread is one of the top Google results when searching for a solution to this problem (and it's still something that isn't supported out of the box with Django).

I've got an alternate solution for the use case where you're only concerned with logged in user sessions, which uses an additional UserSession model to map users to their sessions, something like this:

from django.conf import settingsfrom django.db import modelsfrom django.contrib.sessions.models import Sessionclass UserSession(models.Model):    user = models.ForeignKey(settings.AUTH_USER_MODEL)    session = models.ForeignKey(Session)  

Then you can simply save a new UserSession instance any time a user logs in:

from django.contrib.auth.signals import user_logged_indef user_logged_in_handler(sender, request, user, **kwargs):    UserSession.objects.get_or_create(user = user, session_id = request.session.session_key)user_logged_in.connect(user_logged_in_handler)

And finally when you'd like to list (and potentially clear) the sessions for a particular user:

from .models import UserSessiondef delete_user_sessions(user):    user_sessions = UserSession.objects.filter(user = user)    for user_session in user_sessions:        user_session.session.delete()

That's the nuts and bolts of it, if you'd like more detail I have a blog post covering it.


I found this code snippet

from django.contrib.sessions.models import Sessionfrom django.contrib.auth.models import Usersession_key = '8cae76c505f15432b48c8292a7dd0e54'session = Session.objects.get(session_key=session_key)uid = session.get_decoded().get('_auth_user_id')user = User.objects.get(pk=uid)print user.username, user.get_full_name(), user.email

herehttp://scottbarnham.com/blog/2008/12/04/get-user-from-session-key-in-django/

Have not verified it yet, but looks pretty straight forward.


This is somewhat tricky to do, because not every session is necessarily associated with an authenticated user; Django's session framework supports anonymous sessions as well, and anyone who visits your site will have a session, regardless of whether they're logged in.

This is made trickier still by the fact that the session object itself is serialized -- since Django has no way of knowing which data exactly you want to store, it simply serializes the dictionary of session data into a string (using Python's standard "pickle" module) and stuffs that into your database.

If you have the session key (which will be sent by the user's browser as the cookie value "sessionid"), the easiest way to get at the data is simply to query the Session table for the session with that key, which returns a Session object. You can then call that object's "get_decoded()" method to get the dictionary of session data. If you're not using Django, you can look at the source code (django/contrib/sessions/models.py) to see how the session data is deserialized.

If you have the user id, however, you'll need to loop through all of the Session objects, deserializing each one and looking for one which has a key named "_auth_user_id", and for which the value of that key is the user id.