2011-11-03 48 views
0

具有非重疊值的查詢集我有2種型號:Django的:返回,在2種機型

Pages 
------------ 
Page User 
S500 John 
Gimp John 
WoW  John 

Subscriptions 
------------ 
Page User 
S500 John 

所以在替補模型頁面字段是一個FK到頁面模型。我試圖返回一個查詢集,它將顯示屬於約翰的所有頁面,不包含的訂閱屬於他。

我想是這樣的:

fbpages = Page.objects.filter(user='John').exclude(id__in=[Page.id for Page in Page.subscriptions.filter(Page=Page)]) 

我想我接近,但我不知道在那裏我與查詢走錯了。

我不喜歡的東西:

current_subs = Subscriptions.objects.filter(user='John') 
pages = Page.objects.filter(user='John').exclude(id__in=[subs.Page.id for subs in current_subs]) 

其作品,但我怎麼合併成1個查詢?

回答

1
pages = Page.objects.filter(user='John').exclude(subscriptions__user="John") 
0

您可以使用外鍵的相反方向更直接地執行此操作。如果你還沒有設置reverse_name,我想這應該看起來像

pages = Page.objects.filter(user='John').filter(subscriptions_set=None)