2013-03-06 74 views
2

我有一個關於fieldmeasurement的字段'startDate'類型爲datetime的元數據表。當沒有信息可用時,該值可以爲空。我想在此字段上運行一些統計數據,並獲取最小值和最大值(即所有記錄中可用的最早測量值)。 Max沒有問題,但對於Min,聚合返回None(空字段中的值)。django過濾日期時間=無

所以我的想法是在運行聚合之前從查詢集中過濾這些記錄。我找不到如何做到這一點。

我已經試過

oldestdate = Measurement.object.filter(startDate__isNull=True).aggregate(Min('startDate'))['startDate__min'] 

任何想法是錯的,或者如何做到這一點?

編輯: 一些更多的測試表明:

Measurement.objects.filter(startDate_isnull=True) -> only records with startDate = None 
Measurement.objects.filter(startDate_isnull=False) -> all records, also the ones with startDate = None. 
Measurement.objects.exclude(startDate__isnull=False) -> only records with startDate = None 
Measurement.objects.exclude(startDate__isnull=True) -> shows all records (also startDate == None) 

回答

2
oldestdate = Measurement.objects.filter(
    startDate__isnull=True).aggregate(min_date=Min('startDate')) 

# or to make it small: 

oldestdate = Measurement.objects.filter(startDate__isnull=True 
    ).aggregate(min_date=Min('startDate'), max_date=Max('startDate')) 
+0

你曾經工作過的節點socketio和Django的 – masterofdestiny 2013-03-06 12:44:43

+0

它不工作。帶有None的字段不會被過濾掉。 oQuery = WindWaveFile.objects.all()oQuery.exclude(startDate__isnull = True).aggregate(Min('startDate'))['startDate__min']。strftime(「%d /%m /%Y%H:%M」 )。 strftime引發了一個異常,因爲(錯誤'NoneType'對象沒有屬性'strftime') – 2013-03-11 12:07:08

+0

正確,在我之前發佈的版本中,尚未添加strftime部分。但是,原始問題依然存在,過濾器無法識別無。 – 2013-03-11 12:16:38