Django Queryset datetime filtering

Viewed 39

I have a set of study sessions that have date and time details and I was wondering how I could go about filtering to just show users upcoming study sessions that are occurring either today or after today in my html file? Here is the model for the session.

class Appointment(models.Model):
    date = models.DateTimeField()
2 Answers

Perhaps something like this:

from datetime import datetime

now = datetime.today()
Appointment.objects.filter(date__gte=now)

You can work with the timestamp of the database with a Now expression [Django-doc]:

from django.db.models.functions import Now

Appointment.objects.filter(date__gte=Now())
Related