Reputation: 65
I am currently trying to count the users that are currently logged in. I tried many things but none of them works.
The last code that i tried is :
def count_currently_logged_in(request):
count = Profile.objects.filter(last_login__startswith=timezone.now() - timezone.timedelta(minutes=20)).count()
return Response(count, status.HTTP_200_OK)
Upvotes: 2
Views: 733
Reputation: 477160
Given your profile contains a DateTimeField
named last_login
(this seems to be the case here), you can count this with:
from django.utils import timezone
def count_currently_logged_in(request):
ago20m = timezone.now() - timezone.timedelta(minutes=20)
count = Profile.objects.filter(last_login__gte=ago20m).count()
return Response(count, status.HTTP_200_OK)
We thus filter the Profile
s to retain only profiles with a last_login
later than or exactly 20 minutes ago, and we count these profiles.
Upvotes: 1