Django: Quick snippet to get user from session ID

This is pretty much a drop-and-go snippet.

def get_user_from_session(session_key):
from django.contrib.sessions.models import Session
from django.contrib.auth.models import User

session = Session.objects.get(session_key = session_key)
uid = session.get_decoded().get('_auth_user_id')
return User.objects.get(pk = uid)

user = get_user_from_session('6117e4e8882448931b3de1f2440243ff')
print user.username, user.get_full_name(),

That's it!

Now, back to being productive...


Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog