繁体   English   中英

如何获得带有用户名的对象?

[英]How to get object with User username?

从URL我得到用户名。 现在,在用户名的帮助下,我也想获取确切的User对象。 这是我的代码:

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

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

如何使其运作?

您需要这样的东西:

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

请访问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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM