So I’m trying to get the last_login time for all the users but I can’t figure out how to do that for a user that isn’t logged in.
Is there any way to get this date for user that isn’t logged in? or does that user.last_login only work for the user that is currently logged in?
last_loginis just aDateTimeFieldstored in theUsermodel provided bycontrib.auth. You can query theUserobjects just like you would any of your own models.To print out the
last_loginfor all users, simply loop through the queryset:Alternatively, to get a specific user’s
last_loginjust query for that particular user:The
last_loginfield is set whenever the user last logged in (or, if they haven’t logged in yet, it is set to the time the user account was created). Regardless, it’s always stored in the database so it’s accessibility has nothing to do with whether the user is currently logged in or not.