它不是很简单,但是我能够解决它。我从我有clean_sessions()的文件中导入了此文件:
from importlib import import_modulefrom django.conf import settings
然后,在函数内部,我加载了SessionStore对象:
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
从那里开始,删除会话非常容易,留下这样的方法:
def clean_sessions(): stored_sessions = Session.objects.all() active_users = Request.objects.active_users(seconds=15) active_users_ids = [user.id for user in active_users] for session in stored_sessions: SessionStore = import_module(settings.SESSION_ENGINE).SessionStore s = SessionStore(session_key=session.session_key) session_uid = session.get_depred().get('_auth_user_id') if not session_uid: s.delete() continue if session_uid not in active_users_ids: ## some pre ## s.delete()从正在使用的任何会话引擎中加载正确的SessionStore非常重要,否则它将无法从两个位置(数据库和缓存)中将其删除。



