如何在django rest框架中过滤嵌套数据?

时间:2016-05-15 12:54:53

标签: django django-rest-framework

在我的详细视图中,我有以下结果

{ // measure object
    "id": 5,
    "name": "co",
    "averages": [ // list of averages
        {
            "start": "2016-05-15T12:18:15Z",
            "end": "2016-05-15T12:18:16Z",
            "value": 22.0,
            "length": 4
        }
    ]
}

我需要在averages过滤的数据中显示,例如20<价值< 30。 怎么实现呢?

查看:

class MeasuresList(viewsets.ModelViewSet):
    serializer_class = serializers.DetailedMeasureSerializer

    def get_queryset(self):
        return Measure.objects.all()

1 个答案:

答案 0 :(得分:0)

从数据库中读取时过滤它

def get_queryset(self):
    return Measure.objects.filter(averages__value__range=(20.0, 30.0))