创建自定义过滤器

时间:2018-10-31 08:01:42

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

目前我的看法如下:

from rest_framework.permissions import AllowAny
from dynamic_rest.viewsets import DynamicModelViewSet
from django_filters.rest_framework import DjangoFilterBackend

from .models import Purchase
from .serializers import PurchaseSerializer


class PurchasesViewSet(DynamicModelViewSet):
    queryset = Purchase.objects.all()
    serializer_class = PurchaseSerializer
    permission_classes = (AllowAny,)
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('name', 'color')

这允许我按准确的namecolor进行过滤。

我的数据有一个date字段(DateField类型)。我想按“季度”创建自定义过滤器,以便进行此类请求:

/purchases/?quarter=2018Q1

在指定的四分之一中提供带有date字段的对象列表。请注意,quarter不是模型字段。

创建这种自定义过滤器的方法是什么?

2 个答案:

答案 0 :(得分:2)

首先,您必须创建 自定义过滤器 以及 过滤器类

import django_filters
from rest_framework.exceptions import ValidationError


class QuarterFilter(django_filters.Filter): # Custom Filter
    def get_quarter_range(self, quarter):
        if quarter == 1:
            return [1, 3]
        elif quarter == 2:
            return [4, 6]
        elif quarter == 3:
            return [7, 9]
        elif quarter == 4:
            return [10, 12]
        else:
            raise ValidationError("quarter value must be range from 1-4")

    def filter(self, qs, value):
        """
        create your custom filtering logi here
        """
        # here the "value" will be the value parsed from URL params, ie "2018Q1"
        try:
            year, quarter = value.split("Q") # spliting the input to get year and quarter value
            qs = qs.filter(date__year=year)

            qs = qs.filter(date__month__range=self.get_quarter_range(quarter))
            return qs
        except:
            return qs


class PurchaseFilter(django_filters.FilterSet): # Custom Filter Class
    quarter = QuarterFilter()

    class Meta:
        model = Purchase
        fields = ['name', 'color', 'quarter']



然后,在具有filterset_class属性的视图类中提及它。那你的看法就像

class PurchasesViewSet(DynamicModelViewSet):
    queryset = Purchase.objects.all()
    serializer_class = PurchaseSerializer
    permission_classes = (AllowAny,)
    filter_backends = (DjangoFilterBackend,)
    # filter_fields = ('name', 'color') ---- Remove this line of code
    filterset_class = PurchaseFilter



注意
我不确定这行,

qs = qs.filter(date__month__range=self.get_quarter_range(quarter))

但是,这是在这种特殊情况下需要进行过滤的方式。

希望这会有所帮助!

答案 1 :(得分:2)

您可以在filter_class中创建自定义过滤器

from rest_framework import filters

class CustomFilter(django_filters.rest_framework.FilterSet):
    class Meta:
        model = Purchase
        fields = ['name', 'color', 'quarter']
    quarter = QuarterFilter(name="quarter")

class QuarterFilter(django_filters.Filter):
    def filter(self, qs, value):
        # create custom in this with value
        return qs.filter(name=value)

class PurchasesViewSet(DynamicModelViewSet):
    queryset = Purchase.objects.all()
    serializer_class = PurchaseSerializer
    permission_classes = (AllowAny,)
    filter_backends = (DjangoFilterBackend,)
    filter_class = CustomFilter