Django Query写作

时间:2013-12-09 08:25:43

标签: python sql django django-queryset

如何以Django Query的形式编写以下SQL查询

SELECT time 
FROM dispatcher_booking_timeline 
WHERE status = "completed" AND bookingId_id =
    (SELECT bookingId_id 
     FROM dispatcher_dispatch
     WHERE status = 0)

提前致谢。

2 个答案:

答案 0 :(得分:1)

假设您的模型使用您的SQL查询映射1-1:

status_zeros = Dispatch.objects.filter(status=0).values_list('pk', flat=True)
BookingTimeline.objects.filter(status="completed", pk__in=status_zeros)

答案 1 :(得分:0)

而不是

booking_timelines = BookingTimeline.objects.filter(status="completed",pk__in=status_zeros)
times_set = set( booking_timelines.values_list('time', flat=True) )