Is there any way to see if a user is inactive for a certain amount of time? For example Twitter sends out an email to it's users after a certain amount of time of inactivity. I would like to implement a similar feature where if a user has been inactive for 30 days, an email is sent "Hello User, check out what your friends having been posting" How can I implement this?
4 Answers
You can write a management command which checks for the last time user logged in and if the days are greater than 30, send an email. (You could implement this as a cron that runs every day)
import datetime
from django.core.management.base import BaseCommand
def compute_inactivity():
inactive_users = User.objects.filter(last_login__lt=datetime.datetime.now() - datetime.timedelta(months=1))
#send out emails to these users
class Command(BaseCommand):
def handle(self, **options):
compute_inactivity()
If you have any other criteria which defines "activity", you can filter your queryset based on that.

- 3,965
- 1
- 30
- 45

- 97,368
- 26
- 197
- 188
-
thanks for the quick reply! I was looking for something like this, I'm going to try this out and see if it works! I'll let you know! – deadlock May 28 '13 at 17:31
-
1With this condition, the inactive users will be notified every day when at least a month passes (every moment the cron job is set). Rather than checking the "less than" condition, it is better to check "equals" condition, i.e. when 30 days passes, the user gets a notification. – Aidas Bendoraitis May 28 '13 at 18:48
-
1well in that case, we would have to consider the date, rather than the datetime. Moreover, it is an idea - the OP could implement it in various ways – karthikr May 28 '13 at 18:49
-
@AidasBendoraitis how can I implement it was you suggested? – deadlock May 29 '13 at 17:16
-
Haven't tested that, but it should be something like: def compute_inactivity(): a_month_ago = datetime.datetime.now() - datetime.timedelta(days=30); inactive_users = User.objects.filter(last_login__year=a_month_ago.year, last_login__month=a_month_ago.month, last_login__day=a_month_ago.day) – Aidas Bendoraitis May 29 '13 at 18:53
-
@AidasBendoraitis It is a different approach. worth making it an answer. – karthikr May 29 '13 at 19:40
-
OK. I added that as a separate answer. – Aidas Bendoraitis May 30 '13 at 22:50
Well, django.contrib.auth.models.User
has a last_login
field which might be useful for you.
Just wherever you want, check the last_login
date of the User
and you'll now how long he's been away of your site.
Hope this helps!

- 29,294
- 6
- 74
- 73
-
hey, thanks for the quick reply, I found all that information on the django admin page, I up voted your answer. Check out karthikr's answer above, it was more of the answer I was looking for! :) – deadlock May 28 '13 at 17:38
-
I'm glad you found you're answer. Mine was only to suggest you what to do. Thanks for the updvote. – Paulo Bu May 28 '13 at 19:38
After reading karthikr's answer and Aidas Bendoraitis' suggestion, I've written the correction solution below. It is very similar to Karthikr's answer except instead of using the __lt rich comparison operator, use the __eq operator:
import datetime
from django.core.management.base import BaseCommand
def compute_inactivity():
inactive_users = User.objects.filter(last_login__eq=datetime.datetime.now() - datetime.timedelta(months=1))
#send out emails to these users
class Command(BaseCommand):
def handle(self, **options):
compute_inactivity()

- 7,048
- 14
- 67
- 115
My approach would be to send notifications to users exactly when 30 days pass since their last login. For this, you will need to create a management command and run it as a cron job daily.
import datetime
from django.core.management.base import BaseCommand
def compute_inactivity():
a_month_ago = datetime.datetime.now() - datetime.timedelta(days=30)
inactive_users = User.objects.filter(
last_login__year=a_month_ago.year,
last_login__month=a_month_ago.month,
last_login__day=a_month_ago.day,
)
#send out emails to these users
class Command(BaseCommand):
def handle(self, **options):
compute_inactivity()

- 3,965
- 1
- 30
- 45