Home > database >  Django filter query using future date-time
Django filter query using future date-time

Time:11-28

How do I filter a query to contain only the objects that have a date-time field that holds a DateTime in the future? Here is my code that is not working:

def matches(request):
    matches= Match.objects.all().filter(match_date_time > timezone.now() )
    context = {'matches': matches}
    return render(request, 'bookie/matches.html', context)

match_date_time is a model field in the Match model but I get an error that it's not defined.

CodePudding user response:

You use the __gt lookup [Django-doc] to filter with greater than:

def matches(request):
    matches = Match.objects.filter(match_date_time__gt=timezone.now())
    return render(request, 'bookie/matches.html', {'matches': matches})
  • Related