[英]Get last_login time for a certain user? (django)
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. 我有代码
User.objects.values_list('last_login',flat= True)
该代码为我提供了所有用户的所有最近一次登录的列表,但我不确定如何限制特定用户的操作。 I tried code along the lines of User.objects.get(username='user1').values_list('last_login',flat= True)
But that didn't work. 我尝试了
User.objects.get(username='user1').values_list('last_login',flat= True)
但这User.objects.get(username='user1').values_list('last_login',flat= True)
。 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? 我想我需要在第一组paren和values_list之间添加一些内容,但是我不确定我将在其中放置什么以便它们链接起来?
Try this: 尝试这个:
user = User.objects.get(username='user1')
last_login = user.last_login
As you look at Django docs values_list
is under Methods that return new QuerySets . 当您查看Django docs docs
values_list
,该方法位于返回新QuerySet的Methods下。 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, 并且last_login是
User
模型的字段,因此您可以通过以下方式直接访问它:
User.objects.get(username='user1').last_login
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.