我有一个按钮,它应该出现在模型对象的start_date前一天。在我看来,我有一个函数告诉我对象的start_date或end_date是否现在。
def has_engagement(self):
from rental.models import Inquiry
inquiries = Inquiry.objects.filter(inquiryequipment__equipment=self).filter(start_date__lte= timezone.now()).\
filter(end_date__gte=timezone.now()).filter(status="CO")
if not inquiries:
return False
# no current confirmed engagement
else:
return True
# there is a current confirmed engagement
而不是这个,我需要检查今天是开始日期还是结束日期的前一天。 有任何想法吗?感谢
答案 0 :(得分:1)
你可以使用timedelta如下:
previous = timezone.now() - timedelta(1) # returns date object of previous day
next = timezone.now() + timedelta(1) # returns date object of next day
Inquiry.objects.filter(start_date__lte=previous, end_date__gte=next, status="CO", inquiryequipment__equipment=self)
请注意,您可以使用逗号分隔
过滤查询集和查询答案 1 :(得分:1)
with out date formate
previous = timezone.now() - timedelta(1) # returns date object of previous day
next = timezone.now() + timedelta(1) # returns date object of next day
Inquiry.objects.filter(start_date__lte=previous, end_date__gte=next, status="CO", inquiryequipment__equipment=self)
with date formate
previous = (timezone.now() - timedelta(1)).strftime('%Y-%d-%m') # returns date object of previous day
next = (timezone.now() + timedelta(1)).strftime('%Y-%d-%m') # returns date object of next day
Inquiry.objects.filter(start_date__lte=previous, end_date__gte=next, status="CO", inquiryequipment__equipment=self)