1
class Event(models.Model):
    name = models.CharField(max_length=255)
    start = models.DateTimeField()
    stop = models.DateTimeField()

now = timezone.now() # 08:30 am
qs = Event.objects.filter(start__gte=now) #but this exclude `event1`

Events objects:

name   start             stop
event1 20.11.2015 08:15  20.11.2015 09:30
event2 20.11.2015 09:30  20.11.2015 10:25

What is the best way to get events including the currently ongoing?

Moe Far
  • 2,742
  • 2
  • 23
  • 41
user4812479812
  • 583
  • 1
  • 6
  • 13

2 Answers2

1

Based on your comment to Anush, you probably want to try the following:

now = timezone.now().date()
qs = Event.objects.filter(
   start__year__lte=now.year,
   start__month__lte=now.month,
   start__day__lte=now.day,
)

Unfortunately the current stable django version (1.8) does not provide the ability to truncate datetime fields to dates, which is why you need to specify year, month and day seperately.

The latest dev version as of this post's writing (1.9) provides the __date lookup which should solve this.

Michael Aquilina
  • 5,352
  • 4
  • 33
  • 38
0

try:

now = timezone.now()
qs = Event.objects.filter(start__lte=now, end__gte=now)
Anush Devendra
  • 5,285
  • 1
  • 32
  • 24