2016-08-08 102 views
0

Tastypie小數和日期過濾器下面LTEGTE過濾查詢返回0對象:沒有工作

curl http://localhost/river/river/?runoff__lte=100.0&runoff__gte=150.0 
curl http://localhost/river/river/?runoff__lte=100&runoff__gte=150 
http://localhost/river/river/?dt_timestamp__lte=2015-01-01T03:00&dt_timestamp__gte=2015-01-07T18:00&format=json 

這裏的models.py

class River(models.Model): 
    dt_timestamp = models.DateTimeField() 
    stage = models.DecimalField(max_digits=10, decimal_places=3, blank=True, null=True) 
    runoff = models.DecimalField(max_digits=10, decimal_places=3) 

api.py

class RiverResults(ModelResource): 
    class Meta: 
     queryset = River.objects.all() 
     resource_name = 'river' 
     authorization = Authorization() 
     filtering = { 
      'user': ALL_WITH_RELATIONS, 
      'dt_timestamp': ALL 
      'stage': ALL, 
      'runoff': ALL, 
     } 

在settings.py USE_TZ =假

正在運行PostgreSQL的9.3,Django的1.6和Tastypie 0.12.2。 不知道什麼是做錯了。

問候, 艾倫

回答

0

我想你需要選擇其中的河流是runoff 100和150之間2015-01-01T03之間或dt_timestamp:00和2015-01-07T18:00。在這種情況下,嘗試:

http://localhost/river/river/?runoff__gte=100.0&runoff__lte=150.0 
http://localhost/river/river/?runoff__gte=100&runoff__lte=150 
http://localhost/river/river/?dt_timestamp__gte=2015-01-01T03:00&dt_timestamp__lte=2015-01-07T18:00 

如果您需要選擇河流徑流量都小於100或大於150,那麼你需要覆蓋build_filters功能:

def build_filters(self, filters=None): 
    qs_filters = super(RiverResults, self).build_filters(filters) 
    if filters.get('runoff_not_between') is not None: 
     runoff_not_between = filters.get('runoff_not_between').split(',') 
     qs_filters = qs_filters.update(Q(runoff__lte=runoff_not_between[0]) | Q(runoff__gte=runoff_not_between[1])) 
    return qs_filters 

及用途:

http://localhost/river/river/?runoff_not_between=100.0,150.0 
http://localhost/river/river/?runoff_not_between=100,150