How to lookup django session for a particular user?
来源:互联网 发布:高中文言文朗读软件 编辑:程序博客网 时间:2024/06/05 09:49
https://stackoverflow.com/questions/235950/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.
- How to lookup django session for a particular user?
- How to kill a particular user terminal on Linux
- How to Find which Session is Holding a Particular Library Cache Lock (文档 ID 122793.1)
- How to Find which Session is Holding a Particular Library Cache Lock
- How to Find which Session is Holding a Particular Library Cache Lock [ID 122793.1]【每日一译】--2012-11-13
- How to Find Bapi for Particular Transaction in SAP
- How to Find Bapi for Particular Transaction in SAP
- How do I find the PublicKeyToken for a particular dll?
- How to remove a block from the particular page?
- How to achieve success in a particular field
- How to Extend Django User Model
- How to add a user and set environments for the user?
- How to sort an array in Ruby to a particular order?
- Rapid Contextual Design: A How-to Guide to Key Techniques for User-Centered Design
- How will you recursively search for presence of a particular text under multiple folders unix?
- How to Check Missing Authorisation for User
- How to create a user in PostgreSQL
- How to create a remote desktop user
- 欢迎使用CSDN-markdown编辑器
- Laravel验证简记
- mysql mapper 参数判断
- mybatis使用pagehelper分页报错java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot be cast
- ce
- How to lookup django session for a particular user?
- 设计模式 外观模式 一键电影模式
- 一个函数作为另一个函数的参数
- 802.11无线网络学习(二):802.11 网络概论
- 51nod 1055 最长等差数列
- Angular中的ng-bind和ng-model区别
- elasticsearch5.4.2启动报错及对应解决方案
- 000
- IEnumerable.Select和where