한 달 미만의 모든 품목을 얻습니다
-
13-09-2019 - |
문제
한 달 전에 Django에서 날짜가있는 모든 객체를 얻을 수있는 방법이 있습니까?
같은 것 :
items = Item.objects.filter(less than a month old).order_by(...)
해결책
"달"에 대한 당신의 정의는 무엇입니까? 30 일? 31 일? 그 과거에는 이렇게해야합니다.
from datetime import datetime, timedelta
last_month = datetime.today() - timedelta(days=30)
items = Item.objects.filter(my_date__gte=last_month).order_by(...)
Advantange를 취합니다 gte 현장 조회.
다른 팁
items = Item.objects.filter(created_date__gte=aMonthAgo)
Amonthago는 DateTime과 Timedelta에 의해 계산됩니다.
제휴하지 않습니다 StackOverflow