Skip to content
Advertisement

how to compare only date from a model’s datetimefield with current date?

I want to use Model.objects.filter(datetime_lte=datetime.datetime.now.date()) How exactly can I achieve this? I am using django 1.6.5. I want only records of current date. This will give all previous day’s records also

Advertisement

Answer

You can use the __range field lookup:

start = datetime.date.today()
end = start + datetime.timedelta(days=1)
Model.objects.filter(datetime__range=(start, end))
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement