1

这是我的模型:

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)

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

4

1 回答 1

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)
于 2012-10-17T03:33:19.823 回答