如何在Django REST Framework API响应中使用多标记过滤器?

mklgxw1f  于 2022-12-27  发布在  Go
关注(0)|答案(1)|浏览(126)

首先,如果我没有用更好的措辞问问题,我道歉。我正在构建一个DRF应用程序,我有Products模型,ProductSerializerProductsSummaryView。此外,我有工作空间模型(不打算讨论它,因为它与问题无关)。现在,我想在成功响应中返回那些产品详细信息,这些详细信息附加在特定的工作区中,根据一些参数过滤。
我所做的:我定义了filterset_fieldsfilter_backends。它工作正常。例如,如果我转到{{domain}}/path/?country__title=Japan这个URL,它会过滤日本的数据并显示给我。但问题是,我想使用多标记过滤,比如说,如果我想查看日本和韩国的详细信息,那么我想使用{{domain}}/path/?country__title=Japan&country__title=Korea这样的代码,但是它不起作用,它只是返回所有的细节。我甚至尝试在URL中添加空列表格式,并尝试了这个URL {{domain}}/path/?country__title[]=Japan&country__title[]=Korea,但它仍然不起作用。
有人能帮我一下吗?
我的Product型号是:

class Product(BaseModel):
    """Product model to store all prices of a product and
    related brand and countries it's available in"""
    product = models.CharField(max_length=255)
    country = models.ForeignKey(Country, null=True, on_delete=models.DO_NOTHING)
    fzg_segment = models.ForeignKey(
        FZGSegment, on_delete=models.DO_NOTHING, null=True, blank=True
    )

我的ProductSerializer串行器是:

class ProductSerializer(serializers.ModelSerializer):
    """Serializer for the part object"""
    fzg_segment = FZGSegmentSerializer()

    class Meta:
        model = Product
        fields = (
            "id",
            "product",
            "fzg_segment",
        )
        read_only_fields = ("id",)

我的ProductsSummaryView API是:

class ProductsSummaryView(mixins.ListModelMixin, viewsets.GenericViewSet):
    """Viewset to return data for products in a workspace"""
    serializer_class = serializers.ProductSerializer
    permission_classes = (permissions.IsWorkspaceAdminPermission,)
    pagination_class = StandardSetPagination
    filter_backends = [DjangoFilterBackend]
    filterset_fields = [
        "fzg_segment__title",
        "country__title",
    ]

    def get_queryset(self) -> models.Product:
        """Returns queryset of all the products of a workspace"""
        workspace = self.kwargs["workspace"]
        workspace = get_object_or_404(models.Workspace, pk=workspace)
        return workspace.products.all()

有人能帮助我,让我知道我需要做什么,以实现所需的功能?

    • 更新:**我已创建filters.py文件,如下所示:
# filters.py

import django_filters
from .models import Product

class ProductFilter(django_filters.FilterSet):
    fzg_segment__title = django_filters.CharFilter(field_name="fzg_segment__title", lookup_expr='in')
    country__title = django_filters.CharFilter(field_name="country__title", lookup_expr='in')

    class Meta:
        model = Product
        fields = [
            "fzg_segment__title",
            "country__title",
        ]

我已经更新了我的views.py如下:

# views.py

class ProductsSummaryView(mixins.ListModelMixin, viewsets.GenericViewSet):
    """Viewset to return data for products in a workspace"""
    serializer_class = serializers.ProductSerializer
    permission_classes = (permissions.IsWorkspaceAdminPermission,)
    pagination_class = StandardSetPagination
    filter_backends = [DjangoFilterBackend]
    filterset_class = filters.ProductFilter

    def get_queryset(self) -> models.Product:
        """Returns queryset of all the products of a workspace"""
        workspace = self.kwargs["workspace"]
        workspace = get_object_or_404(models.Workspace, pk=workspace)
        queryset = workspace.products.all()
        queryset = self.filter_queryset(queryset)
        return queryset

我尝试在URL中使用如下过滤器:
{{domain]]/path/?country__title=Japan&country__title=Korea
以及
'{{域] ]/路径/?国家__标题__在=日本&国家__标题__在=韩国''
以及
{{domain]]/path/?country__title__in=Japan,Korea
以及
{{domain]]/path/?country__title[]=Japan&country__title[]=Korea
但一切都是徒劳的,我甚至不能使用单一的过滤器,离开多个过滤器。我做错了什么?

p8ekf7hl

p8ekf7hl1#

您需要使用BaseInFilter,例如:

from django_filters.filters import BaseInFilter
from django_filters import rest_framework as filters

class CharInFilter(BaseInFilter, filters.CharFilter):
    pass

class ProductFilterSet(filters.FilterSet):
    country = CharInFilter(field_name="country__title", lookup_expr='in')

    class Meta:
        model = Product
        fields = ["country"]

参数url如下所示:第一个月

相关问题