繁体   English   中英

Deliciouspie小数和日期时间过滤器不起作用

[英]Tastypie decimal and datetime filters not working

以下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 = False

我正在运行PostgreSQL 9.3 ,Django 1.6和Deliciouspie 0.12.2 不知道在做什么错。

问候,艾伦

我猜您需要选择runoff介于100和150之间或dt_timestamp在2015-01-01T03: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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM