如何使用用户名获取对象?

时间:2014-10-12 08:12:29

标签: python django

我从网址获取用户名。现在有了用户名的帮助,我也希望得到确切的User对象。这是我的代码:

author = UserProfile.objects.get(user.username_iexact = username)

但是,我收到错误:keyword can't be an expression

如何让它发挥作用?

2 个答案:

答案 0 :(得分:3)

你需要这样的东西:

author = UserProfile.objects.get(user__username__iexact='your_username')

https://docs.djangoproject.com/en/1.7/topics/db/queries/#lookups-that-span-relationships

了解详情

答案 1 :(得分:0)

您需要指定如下(use double underscore而不是点):

author = UserProfile.objects.get(user__username__iexact=username)