2 回答
TA贡献1812条经验 获得超5个赞
您必须根据GET参数手动过滤查询集。
class ImageViewSet(viewsets.ModelViewSet):
queryset = Image.objects.all()
serializer_class = ImageSerializer
filter_backends = (DjangoFilterBackend, OrderingFilter,)
filter_fields = ('image_name',)
ordering_fields = ('id','image_name,')
def get_queryset(self):
minheight = self.request.GET.get('minheight')
maxheight = self.request.GET.get('maxheight')
return self.queryset.filter(image_height__gte=minheight, image_height__lt=maxheight)
TA贡献1803条经验 获得超3个赞
一般来说,查询看起来像:
qs = models.Image.objects.filter(image_height__gt=100, image_height__lt=300)
这使用gt和lt字段查找。还有大于或等于 ( gte) 和小于或等于 ( lte) 的变体。
class ImageViewSet(viewsets.ModelViewSet):
serializer_class = ImageSerializer
filter_backends = (DjangoFilterBackend, OrderingFilter,)
filter_fields = ('image_name',)
ordering_fields = ('id','image_name',)
def get_queryset(self):
queryset = Image.objects.all()
minheight = self.request.query_params.get('minheight', '')
maxheight = self.request.query_params.get('maxheight', '')
if(minheight and maxheight):
queryset = queryset.filter(image_height__gt=minheight,
image_height__lt=maxheight)
return queryset
添加回答
举报