【问题标题】:Tastypie decimal and datetime filters not workingTastypie 十进制和日期时间过滤器不起作用
【发布时间】:2016-08-08 09:48:51
【问题描述】:

以下 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 和 Tastypie 0.12.2。 不知道哪里做错了。

问候, 艾伦

【问题讨论】:

    标签: python django postgresql tastypie


    【解决方案1】:

    我想您需要选择 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2020-03-07
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      相关资源
      最近更新 更多