我有一个事件+日期类型的表格设计。
Class Event
...
Class EventDate
...
date = models.DateField()
event = model.ForeignKey(Event)
class Meta:
unique_together = ('date', 'event')
我试图找到不再发生的事件,即从今天开始过期。
我尝试了以下查询集:
Event.objects.filter(dates_set__date__lt =
datetime.date.today()).exclude(dates_set__date__gte = datetime.date.today())
由于子查询,SQL 查询对我来说似乎很奇怪,而且看起来很昂贵。
SELECT … FROM "events" INNER JOIN "event_dates" ON ("events"."id" =
"event_dates"."events_id") WHERE ("event_dates"."date" < '2013-05-02' AND NOT
(("events"."id" IN (SELECT U1."event_id" FROM "event_dates" U1 WHERE (U1."date"
>= '2013-05-02' AND U1."event_id" IS NOT NULL)) AND "events"."id" IS NOT NULL)))
它是否正确?