'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
Solution 1:[1]
You can use the __range
field lookup:
start = datetime.date.today()
end = start + datetime.timedelta(days=1)
Model.objects.filter(datetime__range=(start, end))
Solution 2:[2]
Try this
Model.objects.filter(created_at__date=today)
Solution 3:[3]
Try this
Model.objects.filter(created_at__year = timezone.now().year,
created_at__month=timezone.now().month,
created_at__day=timezone.now().day)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mhawke |
Solution 2 | Shahid Malik |
Solution 3 | Amar |