Django-Filter和查询数组

时间:2015-07-05 05:27:32

标签: python django api django-rest-framework django-filter

我有一个简单的问题:目前我可以这样做从后端获取一个对象:

http://127.0.0.1:8000/api/v1/boats/boats?id=10
http://127.0.0.1:8000/api/v1/boats/boats?home_port=98&id=5

但是我想根据一系列ID或者home_ports列表获得一系列船只,我试过了:

http://127.0.0.1:8000/api/v1/boats/boats?id=10,11
http://127.0.0.1:8000/api/v1/boats/boats?id_in=10,11
http://127.0.0.1:8000/api/v1/boats/boats?id=10,id=11
http://127.0.0.1:8000/api/v1/boats/boats?id=10&id=11

但这些都行不通。使用django-filter执行此操作的最佳方法是什么,如何定义URL规则?

以下是我的观点:

class BoatList(generics.ListCreateAPIView):
    permission_classes = (IsOwnerOrReadOnly,)
    serializer_class = BoatSerializer
    queryset = Boat.objects.all()
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('id', 'home_port',)

我标记的解决方案100%回答了我的问题,但我最终根据我发现的另一个使用过滤器的帖子实现了不同的内容:

class ListFilter(Filter):

    def filter(self, qs, value):
        if not value:
            return qs

        self.lookup_type = 'in'
        values = value.split(',')
        return super(ListFilter, self).filter(qs, values)

class BoatFilter(FilterSet):
    ids = ListFilter(name='id')

    class Meta:
        model = Boat
        fields = ['home_port', 'ids']


class BoatList(generics.ListCreateAPIView):
    permission_classes = (IsOwnerOrReadOnly,)
    serializer_class = BoatSerializer
    queryset = Boat.objects.all()
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = BoatFilter

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

3 个答案:

答案 0 :(得分:5)

doniyor给出的答案非常贴切。但我猜request在使用它时无法使用。

还有另一种方法。您可以覆盖get_queryset方法。这可以按如下方式完成:

class BoatList(generics.ListCreateAPIView):
    permission_classes = (IsOwnerOrReadOnly,)
    serializer_class = BoatSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('id', 'home_port',)

    def get_queryset(self):
        id_list = self.request.GET.getlist("id")
        if not id_list:
            return []
        return Boat.objects.filter(id__in=id_list)

答案 1 :(得分:3)

试试这个:

url:http://127.0.0.1:8000/api/v1/boats/boats?id=10,11

class BoatList(generics.ListCreateAPIView):
    permission_classes = (IsOwnerOrReadOnly,)
    serializer_class = BoatSerializer
    queryset = Boat.objects.filter(id__in=request.GET.getlist('id')) #<------
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('id', 'home_port',)

答案 2 :(得分:1)

您只需创建一个过滤器类

    class NumberInFilter(BaseInFilter, NumberFilter):
          pass


    class myFilter(FilterSet):
          id__in = NumberInFilter(field_name='id', lookup_expr='in')

          class Meta:
                model = Boat

在视图集中使用

           filter_class = myFilter

您可以看到django-filter的此文档: https://django-filter.readthedocs.io/en/master/ref/filters.html?highlight=BaseInFilter