I have a model which describes an event like this:
class Event(models.Model):
date = models.DateField()
time = models.TimeField()
I would like to retrieve all future events (ie date greater than now.date()). However If date is today, I would like to retrieve today’s events with time greater than now.time().
This is what I am doing:
events = Event.objects.filter(date__gte=now.date()).filter(time__gte=now.time()).order_by('-date')
where now = datetime.datetime.now()
But this is wrong, because it gives me an empty set if the only event is tomorrow and its time is less than current time (for example if event is tomorrow at 09:00 and today is 19:00)
is this possible in django?
PS: I would like to avoid iterating through the set.
Use
Qobjects, which allow you to make queries with OR statements.Note that you might want to sort on the
timefield as well: