2013-03-06 4 views
2

는 I는 필드 '의 startDate는'타입의 날짜 인 fieldmeasurement에 메타 데이터 테이블이 없다. 정보가 없으면 값을 비울 수 있습니다. 이 필드에 대해 몇 가지 통계를 실행하고 최소값과 최대 값을 얻으려고합니다 (즉, 모든 레코드에서 가장 오래된 측정 값을 사용할 수 있음). Max에서는 문제가 없지만 Min에서는 집계가 None (빈 필드의 값)을 반환합니다.장고 = 없음

그래서 내 생각은 집계를 실행하기 전에의 검색어에서 이러한 레코드를 필터링하는 것입니다. 나는 이것을하는 방법을 찾을 수 없습니다.

나는

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에 근무하고 그것은 작동하지 않습니다 – masterofdestiny

+0

을 장고 한 : 좀 더 테스트는 것을 알 수있다. 없음이있는 필드는 필터링되지 않습니다. oQuery WindWaveFile.objects.all =() oQuery.exclude (startDate__isnull = TRUE) .aggregate (분 ('의 startDate')) 'startDate__min']의 strftime ("%의 D/%의 m/%의 Y %의 H %의 M".). 의 strftime 때문에 그 예외 (오류 'NoneType'개체가 어떤 속성 '의 strftime을'없음) –

+0

올바른, 내가 전에 게시 된 버전에서의 strftime 부분이 아직 추가되지 않았습니다를 발생시킵니다. 그러나 원래의 문제는 남아 있으며 필터는 없음을 인식하지 못합니다. –

관련 문제