5👍
✅
You have to just write a valid filter like this :
from datetime import datetime
today = datetime.today()
year = today.year
month = today.month
day = today.day
meca = Meca.objects.filter(created_at__year=year,
created_at__month=month, created_at__day=day)
2👍
I think the main reason is because you trying to compare dt
field with date
.
You can use prefixes for a field.
Or you can compare your ‘today’ value as dt
(just an example):
today = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
records = Maca.objects.filter(created_at >= today)
- [Django]-ValueError: invalid literal for int() with base 10:
- [Django]-Invalid embedded document instance provided to an EmbeddedDocumentField
1👍
there many ways to get today data from database.
year = datetime.datetime.now().strftime('%y')
month = datetime.datetime.now().strftime('%m')
day = datetime.datetime.now().strftime('%d')
filtereddata = Maca.objects.filter(created_at__year=year,created_at__month=month, created_at__day=day )
the second way is to filter with contains
filtereddata = Maca.objects.filter(created_at__contains=datetime.today().date())
if datetime.datetime not work. just remove one datetime only use single datetime
0👍
You can simply use __date
like this.
Maca.objects.filter(created_at__date=datetime.today().date())
- [Django]-"ImportError: No module named cv2" when running Django project from PyCharm IDE
- [Django]-Logging Django Haystack search keyword
- [Django]-How can I resolve this linting error? "Consider adding meta keywords. <html lang="en-US"> (H031)"
Source:stackexchange.com