我有一个 django 查询,它给了我一个错误的查询集。从以下单个查询中,它应该给我 1 - 0 = 1,但它给了我 0 个结果。为什么会这样?
>>> MessageThread.objects.filter(message__recipient=p2)
[<MessageThread: message thread one>]
>>> MessageThread.objects.filter(message__status='deleted', message__recipient=p2)
[]
>>> MessageThread.objects.filter(message__recipient=p2)
.exclude(message__status='deleted', message__recipient=p2)
[]
我将如何构建查询以获取 query1 - query2?这是我在sql中需要的:
SELECT * FROM messaging_messagethread
WHERE id NOT IN
(SELECT DISTINCT thread_id FROM messaging_message
WHERE status = 'deleted' AND recipient_id=4)