问题
I have the code User.objects.values_list('last_login',flat= True) Which gives me a list of all of the last logins for all of the users but I'm unsure how you slim that done to a specific user. I tried code along the lines of User.objects.get(username='user1').values_list('last_login',flat= True) But that didn't work. I think I need something between the first set of paren's and values_list but I not sure what I would put there in order for them to link up?
回答1:
Try this:
user = User.objects.get(username='user1')
last_login = user.last_login
回答2:
As you look at Django docs values_list is under Methods that return new QuerySets. So it is not applicable on single object as you did. And last_login is field of User model so you can access it directly by,
User.objects.get(username='user1').last_login
来源:https://stackoverflow.com/questions/11352495/get-last-login-time-for-a-certain-user-django