2011-06-03 127 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