我在服务模型中有一个DateTimeField,定义如下:
pub_date = models.DateTimeField(default=timezone.now,blank=True)
我想在我的代码中创建一些过期逻辑,以便超过2个月的服务到期。在我看来,只会显示那些未过期的服务。
例如,我想在视图中执行此操作:
views.py
class display_services:
services = Service.objects.all()
valid_services = []
for service in service
**If Service is Not Expired***
valid_services.append(service)
endfor
...
答案 0 :(得分:1)
您只需按pub_date
from datetime import datetime, timedelta
class DisplayServices(ListView):
queryset = Service.objects.filter(pub_date__gte=datetime.now()-timedelta(days=60))
...
答案 1 :(得分:0)
我认为这就是你想要的:
import datetime as dt
two_months_ago = dt.date.today() + dt.timedelta(days=-60)
valid_services = Service.objects.filter(pub_date__gte=two_months_ago)