Django休息框架DjangoFilterBackend icontains不起作用

时间:2016-04-06 17:06:17

标签: django django-rest-framework

我有一个约会时间" 2016-03-29T01:45:01.419731Z"在数据库中

这是我的过滤器:

class TaipeiTimeFilter(filters.FilterSet):
    taipei_time = django_filters.DateTimeFilter(name="update_time", lookup_type='icontains')

    class Meta:
        model = DataData 
        fields = ['taipei_time',]

class DataList(generics.ListAPIView):
    queryset = DataData.objects.all()
    serializer_class = DataSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = TaipeiTimeFilter

但是当我查询http://127.0.0.1:8000/api/data/?taipei_time=2016-03-29时 它一无所获。我的过滤器出了什么问题?

1 个答案:

答案 0 :(得分:1)

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Storyboard x:Key="GradientChaser" RepeatBehavior="Forever"> <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(LinearGradientBrush.StartPoint)" Storyboard.TargetName="rectangle"> <EasingPointKeyFrame KeyTime="0:0:0.5" Value="0.855,0.148"/> <EasingPointKeyFrame KeyTime="0:0:1" Value="0.852,0.855"/> <EasingPointKeyFrame KeyTime="0:0:1.5" Value="0.148,0.855"/> <EasingPointKeyFrame KeyTime="0:0:2" Value="0.144,0.149"/> <EasingPointKeyFrame KeyTime="0:0:2.5" Value="0,0"/> </PointAnimationUsingKeyFrames> <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(LinearGradientBrush.EndPoint)" Storyboard.TargetName="rectangle"> <EasingPointKeyFrame KeyTime="0:0:0.5" Value="0.145,0.852"/> <EasingPointKeyFrame KeyTime="0:0:1" Value="0.148,0.145"/> <EasingPointKeyFrame KeyTime="0:0:1.5" Value="0.852,0.145"/> <EasingPointKeyFrame KeyTime="0:0:2" Value="0.856,0.851"/> <EasingPointKeyFrame KeyTime="0:0:2.5" Value="0,1"/> </PointAnimationUsingKeyFrames> </Storyboard> <Storyboard x:Key="GradientChaserOverlay" RepeatBehavior="Forever"> <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(LinearGradientBrush.StartPoint)" Storyboard.TargetName="rectangle2"> <EasingPointKeyFrame KeyTime="0:0:0.5" Value="0.146,0.146"/> <EasingPointKeyFrame KeyTime="0:0:1" Value="0.502,-0.001"/> <EasingPointKeyFrame KeyTime="0:0:1.5" Value="0.85,0.142"/> <EasingPointKeyFrame KeyTime="0:0:2" Value="0.863,0.845"/> <EasingPointKeyFrame KeyTime="0:0:2.5" Value="-0.001,0.498"/> </PointAnimationUsingKeyFrames> <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(LinearGradientBrush.EndPoint)" Storyboard.TargetName="rectangle2"> <EasingPointKeyFrame KeyTime="0:0:0.5" Value="0.854,0.854"/> <EasingPointKeyFrame KeyTime="0:0:1" Value="0.498,1.001"/> <EasingPointKeyFrame KeyTime="0:0:1.5" Value="0.15,0.858"/> <EasingPointKeyFrame KeyTime="0:0:2" Value="0.137,0.155"/> <EasingPointKeyFrame KeyTime="0:0:2.5" Value="1.001,0.502"/> </PointAnimationUsingKeyFrames> </Storyboard> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard Storyboard="{StaticResource GradientChaser}"/> <BeginStoryboard Storyboard="{StaticResource GradientChaserOverlay}"/> </EventTrigger> </Window.Triggers> <Grid> <Rectangle x:Name="rectangle" Width="250" Height="250" HorizontalAlignment="Center" VerticalAlignment="Center" StrokeThickness="10"> <Rectangle.Stroke> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="Red" Offset="0"/> <GradientStop Color="Transparent" Offset="1"/> </LinearGradientBrush> </Rectangle.Stroke> </Rectangle> <Rectangle x:Name="rectangle2" Width="250" Height="250" HorizontalAlignment="Center" VerticalAlignment="Center" StrokeThickness="10"> <Rectangle.Stroke> <LinearGradientBrush EndPoint="1,0.501" StartPoint="0,0.499"> <GradientStop Color="White" Offset="0.35"/> <GradientStop Color="Transparent" Offset="0.342"/> </LinearGradientBrush> </Rectangle.Stroke> </Rectangle> </Grid> </Window> 用于近似字符串匹配。您正在尝试进行近似日期时间匹配。为此,您将需要一系列日期时间。我使用了为此目的定义的自定义过滤器:

icontains

使用方法如下:

from django.db.models import Q
import django_filters

class RangeFilter(django_filters.Filter):
    def filter(self,qs,value):

        tokens = value.split('..')
        if len(tokens) != 2:
            return qs
        (begin, end) = tokens
        if begin == '' and end == '':
            return qs
        if begin != '' and end != '':
            return qs.filter(Q(**{'%s__range'%self.name:(begin,end)}))
        elif begin != '':
            return qs.filter(Q(**{'%s__gte'%self.name:begin}))
        elif end != '':
            return qs.filter(Q(**{'%s__lte'%self.name:end}))

,查询如下:

taipei_time = RangeFilter(name="update_time")