Django在DateTime上过滤week_day

时间:2015-09-08 15:39:19

标签: django django-queryset

我尝试使用过滤器week_day作为记录here

  

week_day¶

For date and datetime fields, a ‘day of the week’ match. Allows chaining additional field lookups.

Takes an integer value representing the day of week from 1 (Sunday) to 7 (Saturday).
entries = Booking.objects.filter(date__week_day__gte=2)

但这给了我错误:

Unsupported lookup 'week_day' for DateField or join on the field not permitted.

为什么这不起作用?文件显示在那里..

2 个答案:

答案 0 :(得分:3)

使用week_day__gte将在Django 1.9中提供,但在Django 1.8中无法使用。您已关联development docs1.8 docs are here

答案 1 :(得分:2)

没有那么多工作日,而Alasdair的答案中提到的查询week_day__gte尚不可用。

要解决该问题,您可以单独指定每个工作日:

import operator

entries = Booking.objects.filter(reduce(operator.or_, [Q(date__week_day__gte=weekday) for weekday in [2, 3, 4, 5, 6, 7]]))