如何在Django中使用逗号分隔的参数过滤查询(DRF过滤)

时间:2018-06-03 07:52:30

标签: django django-models django-rest-framework

请问我的问题不是重复的,我已经搜索过但没有一致的结果。 所以我用djangorestframework

建立一个restfull api

我想要像

这样的查询
http://localhost:8000/api/products/?category_name_fr=shirt,shoes

查找category_name_fr包含衬衫或鞋子的所有产品

为了在谷歌搜索后执行此操作,我编写了一个自定义的django过滤器类(MultiValueCharFilter),但过滤器实际上并不像我想要的那样。 当我像上面那样进行查询时,它会返回ProductTable中的所有产品。但是,当我像下面这样进行查询时,过滤正确完成

http://localhost:8000/api/products/?category_name_fr=shirt

以下是我的filters.py文件的源代码,产品类是ProductList

    from django_filters import rest_framework as filters
    from .models import  ProductList
    from django_filters import Filter
    from django_filters.fields import Lookup


    class MultiValueCharFilter(filters.BaseCSVFilter, filters.CharFilter):

        def filter(self, qs, value):
            # value is either a list or an 'empty' value
            values = value or []

            print(values)

            for value in values:
                qs = super(MultiValueCharFilter, self).filter(qs, value) | qs
            return qs

class ProductListFilter(filters.FilterSet):
    min_price = filters.NumberFilter(name="price", lookup_expr='gte')
    max_price = filters.NumberFilter(name="price", lookup_expr='lte')
    category_name_fr = MultiValueCharFilter(name="category_name_fr", lookup_expr='icontains')
    category_name_en = MultiValueCharFilter(name="category_name_en", lookup_expr='icontains')
    collection_name_fr = MultiValueCharFilter(name="collection_name_fr", lookup_expr='icontains')
    collection_name_en = MultiValueCharFilter(name="collection_name_en", lookup_expr='icontains')
    name_en = MultiValueCharFilter(name="name_en", lookup_expr='icontains')
    name_fr = MultiValueCharFilter(name="name_fr", lookup_expr='icontains')

    class Meta:
        model = ProductList
        fields = ['sale', 'sold', 'min_price', 'max_price', 'category_name_fr', 'name_en', 'name_fr', 
        'category_name_en', 'collection_name_fr', 'collection_name_en']

有关更多信息,这是视图ListAPIView

的代码
from rest_framework import viewsets, generics, filters as rf_filters
from ..models import Product,  Collection, ProductList
from ..serializers import ProductSerializer, CollectionSerializer, ProductListSerializer
from django_filters import rest_framework as filters
from ..filters import ProductFilter, ProductListFilter
from rest_framework.pagination import PageNumberPagination
from rest_framework.views import APIView
from rest_framework.response import Response


class StandardResultsSetPagination(PageNumberPagination):
    page_size = 4
    page_size_query_param = 'page_size'
    max_page_size = 15

class ProductList(generics.ListAPIView):
    queryset = ProductList.objects.all()
    serializer_class = ProductListSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = ProductListFilter
    serializer_class = ProductListSerializer
    ordering_fields = ('price', 'created_at')
    pagination_class = StandardResultsSetPagination

1 个答案:

答案 0 :(得分:0)

您需要编写一个自定义的filter&filterset类,如下所示

filterset.py

from django_filters import Filter, FilterSet
from django_filters.constants import EMPTY_VALUES

class ListFilter(filters.Filter):
    def filter(self, qs, value):
        if value in EMPTY_VALUES:
            return qs
        value_list = value.split(",")
        qs = super().filter(qs, value_list)
        return qs

class ProductListFilter(filters.FilterSet):
    category_name_fr = ListFilter(field_name="category_name_fr", lookup_expr="in")
    # other fields

views.py

from django_filters.rest_framework import DjangoFilterBackend

class ProductList(generics.ListAPIView):
  filter_backends = (DjangoFilterBackend,)
  filterset_class = ProductListFilter
  # other attr settings

参考:https://django-filter.readthedocs.io/en/stable/guide/rest_framework.html#adding-a-filterset-with-filterset-class