Soliman
Soliman

Reputation: 371

How I can get the number of online users in Django

How I can get the (count) number of online users in Django and display the number in the admin screen

should I add Boolean field in the models

is_online = models.BooleanField()

or there is another way

Upvotes: 0

Views: 731

Answers (1)

Ren
Ren

Reputation: 4680

I recommend you use the django cache:

from django.core.cache import cache

With it you can store a value for each user that is determined to be online and give it an expiration time so that the record is removed when the user is not considered online anymore.

Ideally this should be made as a middleware, so that the cache is updated on every user interaction with your API.

Then when you need to know if a user is online, you only need to attempt to retrieve the record from the cache, say record online_user_pk_1. If the record exists, it's online, if not, then it's not online.

Upvotes: 1

Related Questions