我有两个型号,Design和Profile.配置文件在settings.py中作为要与User模型一起使用的配置文件连接.所以我可以通过它访问它user.get_profile().
每个Design实例都有一个author属性为ForeignKey to User.
所以,当我是任何视图时,我可以通过以下方式获取screenname(Profile的属性):
user.get_profile().screenname
但SEARCH BY FILTER对此属性的语法是什么?我目前拥有的:
designs = Design.objects.filter(author__userprofile__screenname__icontains=w)
这不起作用.思考?
如果您的配置文件类名为Profile,并且您没有使用ForeignKey的related_name属性自定义User < - > Profile关系,那么您不应该通过以下方式访问:
designs = Design.objects.filter(author__user__profile__screenname__icontains=w)
Run Code Online (Sandbox Code Playgroud)
用户 - >配置文件跨越一个关系,因此您需要额外的双下划线.