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
```
Asked By: Riccardo

||

Answers:

Create a condition on the basis of instance.user.last_login and current timestamp – timedelta(1). Use python datetime.

Answered By: Ajay K
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.