[英]How to get object with User username?
From the url I get the username. 从URL我得到用户名。 Now with a help of a username a I also want to get exact User object.
现在,在用户名的帮助下,我也想获取确切的User对象。 Here is my code:
这是我的代码:
author = UserProfile.objects.get(user.username_iexact = username)
However, I get a error: keyword can't be an expression
但是,我收到一个错误:
keyword can't be an expression
How to make it work? 如何使其运作?
You need something like this: 您需要这样的东西:
author = UserProfile.objects.get(user__username__iexact='your_username')
. author = UserProfile.objects.get(user__username__iexact='your_username')
。
Read more at https://docs.djangoproject.com/en/1.7/topics/db/queries/#lookups-that-span-relationships 请访问https://docs.djangoproject.com/en/1.7/topics/db/queries/#lookups-that-span-relationships了解更多信息
您需要指定如下( 使用双下划线代替点):
author = UserProfile.objects.get(user__username__iexact=username)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.