0

我有一周的开始日和一周的结束日作为配置模型中的选择

class config(models.Model)
...
    DAYS_OF_WEEK = (
    (0, 'Monday'),
    (1, 'Tuesday'),
    (2, 'Wednesday'),
    (3, 'Thursday'),
    (4, 'Friday'),
    (5, 'Saturday'),
    (6, 'Sunday'),
    )
    startday = models.Charfield(max_length = 1, choices=DAY_OF_WEEK)
    startday = models.Charfield(max_length = 1, choices=DAY_OF_WEEK)
    starttime = models.TimeField()
    endtime = models.Timefield()

我有一个单独的模型,它有一个包含时间戳事件的日期时间字段。

class event(models.Model)
...
event_timestamp = models.DateTimeField()

如何获取事件模型在开始日期/时间范围内的所有配置模型?

4

2 回答 2

1

我最终这样做了

day =  timestamp.weekday()
config = ZoneConfiguration.objects.get(video_channel = event.videochannel , zone_endday__gte = day, zone_startday__lte = day)

并将开始和结束日期重新定义为整数。

DAYS_OF_WEEK = (
            (0,'Monday'),
            (1,'Tuesday'),
            (2,'Wednesday'),
            (3,'Thursday'),
            (4,'Friday'),
            (5,'Saturday'),
            (6,'Sunday')
            )
于 2012-05-13T17:48:57.940 回答
0

正如 Django 文档所述,您可以按工作日过滤日期时间字段。也就是说,你可以做

Entry.objects.filter(pub_date__week_day=2) # Day 2 is Monday

也许您还可以添加范围过滤器/运算符,如下所示

Entry.objects.filter(pub_date__week_day__range=(2,4)) # Monday to wednesday

这至少适用于日期部分。

于 2012-05-11T20:03:01.757 回答