2010-02-25 41 views
0

我有一個SQL查詢在基於Django的webapp的Postgres數據庫上運行。該查詢針對由Django-Notifications(可重複使用的應用程序)存儲的數據運行,並返回未選擇退出特定通知類型的電子郵件地址列表。將SQL查詢轉換爲適用於應用程序的Django友好格式

我真的很想能夠做的是構建一個應用程序,根據需求做到這一點,所以我正在尋找一個如何轉換SQL的例子,以便它可以在Django視圖內運行,列出格式化的電子郵件列表。在SQL目前是這樣的:

gr_webapp=# select email from emailconfirmation_emailaddress where verified and user_id not in 
(select user_id from notification_noticesetting s join notification_noticetype t on s.notice_type_id = t.id 
where t.label = 'announcement' and not s.send); 

回答

1

您可能必須做出相應的調整儘可能型號名稱去了,因爲你沒有告訴他們你的問題:

users_to_exclude = Noticesetting.objects.filter(send=False, notice_type__label='announcement').values('user') 
emails = Emailaddress.objects.exclude(user__in=users_to_exclude) 
+0

我試圖用的變化你的例子,但它給「錯誤綁定參數0 - 可能不受支持的類型。」 。 users_to_exlude = NoticeSetting.objects.filter(notice_type = '公告')VALUES( '用戶') 電子郵件= EmailAddress.objects.exclude(用戶= users_to_exlude) \t打印電子郵件 – 2010-02-25 21:03:22

+0

我明白了:我是這樣做錯誤 - 如您所說: \t users_to_exlude = NoticeSetting.objects.filter(notice_type ='announcement')。values('user') \t emails = EmailAddress.objects.exclude(user__in = users_to_exlude) – 2010-02-25 21:10:51