Most optimized way to delete all sessions for a specific user in Django?

后端 未结 5 1322
感动是毒
感动是毒 2021-02-01 18:40

I\'m running Django 1.3, using Sessions Middleware and Auth Middleware:

# settings.py

SESSION_ENGINE = django.contrib.sessions.backends.db   # Persist sessions          


        
5条回答
  •  粉色の甜心
    2021-02-01 19:19

    The most efficient way is to store the session id of the user during login. You can access the session ID using request.session._session_key and store it in a separate model which has reference to the user. Now when you want to remove all the sessions of the user, just query this model which will return all the active sessions for the user in question. Now you need to delete only these sessions from the session table. Much better than having to look up all the sessions to filter out just sessions for a particular user.

提交回复
热议问题