本文介绍了Django中带有DateTimeField的到期逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这样定义的服务模型中有一个DateTimeField:
I have a DateTimeField in a Service model defined like this:
pub_date = models.DateTimeField(default=timezone.now,blank=True)
我想在代码中创建一些到期逻辑,以便较早的服务超过2个月到期。我认为,只会显示那些尚未过期的服务。
I want to create some expiration logic in my code so that Services that are older than 2 months expire. In my view, only those services that aren't expired will be displayed.
例如,我想在视图中执行此操作:
For example I want to do this in a view:
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
...
推荐答案
您可以按 pub_date
from datetime import datetime, timedelta
class DisplayServices(ListView):
queryset = Service.objects.filter(pub_date__gte=datetime.now()-timedelta(days=60))
...
这篇关于Django中带有DateTimeField的到期逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!