【问题标题】:Filter for several items of the same field Django过滤同一字段Django的多个项目
【发布时间】:2022-01-16 23:24:08
【问题描述】:

我有这样的看法:

class ProductViewSet(viewsets.ModelViewSet):
    ...
    filter_class = ProductFilter

和产品过滤器:

class ProductFilter(django_filters.FilterSet):
    class Meta:
        model = Product
        fields = ['brand']

问题是当我发送GET /products/?brand=Adidas+Nike 时,我收到错误[ "Select a valid choice. Nike Adidas is not one of the available choices." ]

如何解决它会过滤同一字段的多个项目。

【问题讨论】:

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


【解决方案1】:

您可能需要在此字段上使用MultipleChoiceFilter,默认情况下它将使用 OR 逻辑(耐克或阿迪达斯)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 2018-10-03
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 2010-11-22
    • 2018-06-07
    • 2015-07-18
    相关资源
    最近更新 更多