2012-10-17 18 views
1

這是我的模型:的Django得到更具體的小時範圍內的數據

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

你總是可以把結果拿出來用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) 
+0

它不工作。錯誤說:加入字段'Record_Date'不允許。查找類型是否拼寫錯誤? –

+1

我認爲只能查找年,月和日,對嗎? –

+0

加入錯誤...嗯...好吧,這可能無法正常工作。如果您嘗試多個過濾器,那麼如何? – monkut