我有以下情况:
这是我的views.py
:
def home(request):
date = datetime.date.today()
start_week = date - datetime.timedelta(date.weekday() + 1)
end_week = start_week + datetime.timedelta(6)
week_tasks = Task.object.filter(owner=request.user, start_date__range=[start_week, end_week])
context = {}
context['week_tasks'] = week_tasks
return render(request, 'home.html', context)
此视图检查start_date
(DateField)是否在当前周的范围内。但我在数据库上有另一个字段end_date
,我想检查这个范围的任何值是否在当前一周。
检查例子: 让我们假设当前周是当天的一周17.根据我目前的观点,只有全天活动和会议属于本周。我需要证明所有这些事件属于本周。
Obs。:我不能只检查本周是否start_date
和end_date
,因为我有长事件的情况,这是在一周后结束。
views.py
:
def home(request):
date = datetime.date.today()
if date.isoweekday() == 7:
date = date + datetime.timedelta(1)
start_week = date - datetime.timedelta(date.isoweekday())
end_week = start_week + datetime.timedelta(6)
week_tasks = Task.object.filter(owner=request.user).exclude(end_date__lt=start_week).exclude(start_date__gt=end_week)
context = {}
context['week_tasks'] = week_tasks
return render(request, 'home.html', context)
答案 0 :(得分:3)
周窗口定义为:week_start,week_end
任务由以下内容定义:task_start,task_end
任务与周重叠,如果:
task_start < week_end and task_end >= week_start