Created
April 7, 2018 17:57
-
-
Save LowerDeez/cb6a62ebf33764468f15f465276b89d6 to your computer and use it in GitHub Desktop.
Django. Get List of Current Users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SESSION_EXPIRE_AT_BROWSER_CLOSE = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import User | |
from django.contrib.sessions.models import Session | |
from django.utils import timezone | |
def get_current_users(): | |
active_sessions = Session.objects.filter(expire_date__gte=timezone.now()) | |
user_id_list = [] | |
for session in active_sessions: | |
data = session.get_decoded() | |
user_id_list.append(data.get('_auth_user_id', None)) | |
# Query all logged in users based on id list | |
return User.objects.filter(id__in=user_id_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment