Add conditional filter to ListView Django

740 views Asked by At

I have list of events. Using ListView to render them.

In models.py

def event_date_time(self):
    event_date_time = datetime.combine(self.event_date, self.event_time)
    return event_date_time

In ListView i also have now= datetime.utcnow()

Need to add a filter to queryset.

if now > event_date_time:
     #don't render that event
else:
     #render
1

There are 1 answers

0
zubhav On BEST ANSWER

You cannot use a model function in Django filtering. You should use DateTimeField to store event_date and event_time in one attribute. Then, in your ListView, override the 'get_queryset' function like so:

def get_queryset(self):
    return Event.objects.filter(event_datetime__gte=datetime.now())