[英]How to filter a query by property of user profile in Django?
I have two models, Design
and Profile
. 我有两个模型,
Design
和Profile
。 Profile is hooked up in settings.py as the profile to be used with the User model. 配置文件在settings.py中作为要与User模型一起使用的配置文件连接。 So I can access it via
user.get_profile()
. 所以我可以通过
user.get_profile()
访问它。
And each Design
instance has an author
property that is a ForeignKey to User. 每个
Design
实例都有一个author
属性,它是一个ForeignKey to User。
So, when I'm any view, I can get the screenname (a property of Profile) by: 所以,当我是任何视图时,我可以通过以下方式获取screenname(Profile的属性):
user.get_profile().screenname
But what is the syntax to SEARCH BY FILTER for this property? 但SEARCH BY FILTER对此属性的语法是什么? What I currently have:
我目前拥有的:
designs = Design.objects.filter(author__userprofile__screenname__icontains=w)
This doesn't work. 这不起作用。 Thoughts?
思考?
If your profile class is named Profile, and you haven't customized the User <-> Profile relation using the related_name property of the ForeignKey, then shouldn't you be accessing via: 如果您的配置文件类名为Profile,并且您没有使用ForeignKey的related_name属性自定义User < - > Profile关系,那么您不应该通过以下方式访问:
designs = Design.objects.filter(author__user__profile__screenname__icontains=w)
The User -> Profile spans a relation so you need the extra double underscores. 用户 - >配置文件跨越一个关系,因此您需要额外的双下划线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.