Get last_login time for a certain user? (django)

你离开我真会死。 提交于 2020-04-11 12:21:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!