DjangoFilterBackend有多个ID

时间:2015-06-24 14:48:21

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

我想用django-filter过滤我的模型。如果我按一个id过滤它可以正常工作:

http://localhost:8000/accommodations?accommodationType_id=1

但我不知道如何通过多个ID进行过滤。

http://localhost:8000/accommodations?accommodationType_id=1,2

我的实际ViewSet看起来像这样:

class AccommodationViewSet(viewsets.ReadOnlyModelViewSet):
    """
        REST API endpoint for 'accommodation' resource
    """
    queryset = Accommodation.objects.all()
    serializer_class = AccommodationSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('accommodationType_id', 'name')

我希望有一个解决方案。

3 个答案:

答案 0 :(得分:13)

我为我的问题找到了以下解决方案:)

this tutorial

from webapp.serializers import *
from rest_framework import viewsets
from rest_framework import filters
from django_filters import Filter, FilterSet


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

        # For django-filter versions < 0.13, use lookup_type instead of lookup_expr
        self.lookup_expr = 'in'
        values = value.split(',')
        return super(ListFilter, self).filter(qs, values)


class AccommodationFilter(FilterSet):
    ids = ListFilter(name='id')
    accommodationType_ids = ListFilter(name='accommodationType_id')

    class Meta:
        model = Accommodation
        fields = ['ids', 'accommodationType_ids']


class AccommodationViewSet(viewsets.ReadOnlyModelViewSet):
    """
        REST API endpoint for 'accommodation' resource
    """
    queryset = Accommodation.objects.all()
    serializer_class = AccommodationSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = AccommodationFilter

答案 1 :(得分:1)

我知道这是一个古老的问题,但值得给出更新的答案。

Django过滤器提供者添加了一个名为BaseInFilter的字段,您可以将其与其他过滤器组合以验证内容。

查看文档: https://django-filter.readthedocs.io/en/latest/ref/filters.html#baseinfilter

例如,这将适合您的情况:

from django_filters import rest_framework as filters


class NumberInFilter(filters.BaseInFilter, filters.NumberFilter):
    pass


class AccommodationFilter(filters.FilterSet):
    accommodationType_id_in = NumberInFilter(field_name='accommodationType_id', lookup_expr='in')

    class Meta:
        model = Accommodation
        fields = ['accommodationType_id_in', ]

然后,您将可以按ID列表进行过滤:http://localhost:8000/accommodations?accommodationType_id_in=1,2

答案 2 :(得分:0)

现在有一种更简单的方法可以实现这一目标。在 django-filter documentation 的深处,它提到您可以使用“映射到查找列表的字段名称字典”。

您的代码将像这样更新:

class AccommodationViewSet(viewsets.ReadOnlyModelViewSet):
    """
        REST API endpoint for 'accommodation' resource
    """
    queryset = Accommodation.objects.all()
    serializer_class = AccommodationSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = {
        'accommodationType_id': ["in", "exact"], # note the 'in' field
        'name': ["exact"]
    }

现在在 URL 中,您需要在提供参数列表之前将 __in 添加到过滤器中,它会按您的预期工作:

http://localhost:8000/accommodations?accommodationType_id__in=1,2

关于可用查找过滤器的 django-filter 文档非常糟糕,但 Django documentation 本身中提到了 in 查找过滤器。