将SQL查询转换为Django友好格式
我有一个SQL查询,它在我基于Django的网页应用的Postgres数据库上运行。这个查询是针对Django-Notifications(一个可重用的应用)存储的数据,返回一份没有选择退出特定通知类型的电子邮件地址列表。
我真正想做的是构建一个可以按需执行这个查询的应用,所以我在寻找一个示例,教我如何把这个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 个回答
1
你可能需要根据模型名称做一些适当的调整,因为你在提问时没有展示这些名称。
users_to_exclude = Noticesetting.objects.filter(send=False, notice_type__label='announcement').values('user')
emails = Emailaddress.objects.exclude(user__in=users_to_exclude)