Django获得更具体的小时范围数据

时间:2012-10-17 03:11:01

标签: python django django-queryset

这是我的模特:

class People(models.Model):
    Name = models.CharField(max_length=100)
    Lastname = models.CharField(max_length=100)
    Record_Date=models.DateTimeField()

在views.py

"""Takes an integer value representing the day of week from 1 (Sunday) to 7 (Saturday)."""

    People.objects.filter(Record_Date__week_day=1)

它给了我所有星期日的Record_Date。那样就好。但是,我想在每个星期日数据中获得更具体的时间。例如,每周日10:30-11:55。如何使用查询或其他替代方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

你总是可以把结果拿出来并在python中过滤。

results = People.objects.filter(Record_Date__week_day=1)
filtered = [r for r in results if 55 <= r.Record_Date.minute <= 30 and 10 <= r.Record_Date.hour <= 11]

然后拆分成循环以便于调试:

results = People.objects.filter(Record_Date__week_day=1)
filtered = []
for r in results:
    print r.Record_Date
    if 55 <= r.Record_Date.minute <= 30:
        print r.Record_Date.minute, r.Record_Date.hour
        if 10 <= r.Record_Date.hour <= 11:
            print "Found!"
            filtered.append(r)