How to lookup django session for a particular user?
2017-06-23 18:05
537 查看
https://stackoverflow.com/questions/235950/how-to-lookup-django-session-for-a-particular-user
up vote33down vote | 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 UserSessionmodel to map users to their sessions, something like this: from django.conf import settings from django.db import models from django.contrib.sessions.models import Session class UserSession(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL) session = models.ForeignKey(Session) Then you can simply save a new UserSessioninstance any time a user logs in: from django.contrib.auth.signals import user_logged_in def 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 UserSession def 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. |
相关文章推荐
- How to test for a valid user session in a JSP
- How to add event for usercontorl ?
- How to Support MUI (Multi User Interface) for both Vista and XP
- Linux: How to enable root user in Gnome for VirtualBox Fedora?
- How to set timeout for user when login using zend_auth?
- How to Find which Session is Holding a Particular Library Cache Lock (文档 ID 122793.1)
- How to add a user and set environments for the user?
- ABAP:How to Find Bapi for Particular Transaction in SAP
- How to Find which Session is Holding a Particular Library Cache Lock [ID 122793.1]【每日一译】--2012-11-13
- IntegrationWithDjango - modwsgi - How to use Django with mod_wsgi. - Python WSGI adapter module for Apache. - Google Project Hosting
- [Yii Framework] how to add additional data to the user session
- How to set trace for others sessions, for your own session and at instance level
- How to Check Missing Authorisation for User
- How to create fully custom Role, User, Event, Resource classes for use with the Security and Scheduler modules
- Rapid Contextual Design: A How-to Guide to Key Techniques for User-Centered Design
- How to Analyzing Authorization Checks for SAP User
- How to Find Blocking Session for Mutex Wait Event cursor: pin S wait on X
- How to add a user and set environments for the us
- How to Determine the Blocking Session for Event: 'cursor: pin S wait on X' (文档 ID 786507.1)
- How to Enable SQL_TRACE for Another Session or in MTS Using Oradebug(文档 ID 1058210.6)