How to check when was last time the User logged in?
Question:
I would like to update my model if the user did not logged in the day before .
@receiver(post_save, sender=user_logged_in)
def user_logged_in_streak(sender, instance, *args, **kwargs):
today: date = timezone.now().date()
if instance.user.last_login != today):
UserStatisticStatus.objects.update(day_streak=0)
else:
pass
```
Answers:
Create a condition on the basis of instance.user.last_login and current timestamp – timedelta(1). Use python datetime.
I would like to update my model if the user did not logged in the day before .
@receiver(post_save, sender=user_logged_in)
def user_logged_in_streak(sender, instance, *args, **kwargs):
today: date = timezone.now().date()
if instance.user.last_login != today):
UserStatisticStatus.objects.update(day_streak=0)
else:
pass
```
Create a condition on the basis of instance.user.last_login and current timestamp – timedelta(1). Use python datetime.