Queryset filter/excludes for dates

I have an event + dates type table design.

Class Event
   ...

Class EventDate
   ...
   date = models.DateField()
   event = model.ForeignKey(Event)

   class Meta:
       unique_together = ('date', 'event')

I'm trying to find events that are no longer happening, i.e. expired from today onwards.

I tried the following queryset:

 Event.objects.filter(dates_set__date__lt = 
 datetime.date.today()).exclude(dates_set__date__gte = datetime.date.today())

The SQL query seems odd to me because of the subquery, and it looks expensive.

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)))

Is this correct?

Answers


Logically, the filter and the exclude are the same, so using one or the other should return the results you want.

Another way to do "exclusion" is to use Q objects to make "not" statements. So if you wanted to get all events with status != "expired" and date < today you can use

Event.objects.filter(Q(dates_set__date__lt=datetime.date.today()), ~Q(status="expired"))

Need Your Help

WSDL to Java -client creation error

xsd axis wsdl2java

I am trying to create a web service from the WSDL but is repeatedly giving me the following error. Tried refreshing axis.jar in the build path but is still giving same error whereas I have another ...