Parth Singh
Parth Singh

Reputation: 69

How to keep a list of all active users of the web app?

I am new to Django. I have been trying a store a list of all active ip addresses which are using the web app currently.

My approach is to add a new entry to the database table with the IP address of the user as soon as a person logs in. And keep updating the timestamp column (LastPingTime) in the table with the current time every 3 seconds or so. So the active ip addresses would be all the rows in the table whose LastPingTime is less than or equal to 3 seconds from the current time. But I don't know to go about it. Please help with this. How do I run a periodic function which does this for every new user?​ Thanks in advance.

Upvotes: 1

Views: 256

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477065

You can query for these users with:

from django.utils.timezone import now
from datetime import timedelta

MyModel.objects.filter(LastPingTime__gte=now()-timedelta(seconds=3))

This will return a QuerySet of MyModels that have a LastPingTime that is later than or equal to three seconds ago.

Note: field names are usually written as last_ping_time, not LastPingTime.

Upvotes: 1

Related Questions