2011-06-03 115 views
0

我想在一个视图来检索以下数据:Django的查询许多一对多的关系

user_profile有一个对一个加盟users
user_profile有许多一对多加盟store,通过stores
store有一个一对多加入到targets,通过targets.store

我可以做检索与用户相关联的所有专卖店:

user_profile = request.user.get_profile() 
all_stores = user_profile.stores.all() 

我想检索的是与用户ID user_profile相关的所有目标,大概是通过all_stores结果,但我不确定如何到达那里。

+0

你是在找这个吗? https://docs.djangoproject.com/en/1.3/topics/db/queries/#spanning-multi-valued-relationships – 2011-06-03 20:19:41

+0

@ S.Lott,是的,但我一直无法创建查询;我不知道我应该如何定义过滤器声明。 – urschrei 2011-06-03 20:23:42

回答

0
Target.objects.filter(store__user_profile__user=request.user) 
+0

谢谢。我试图关注向后/向前的关系,而不是以SQL连接的方式思考,我感到困惑。 – urschrei 2011-06-03 20:38:31