我使用django_过滤器和django_rest_框架为我的数据创建rest端点。在
我有一个模型,我想通过传递起始日期和结束日期作为查询参数进行筛选。到目前为止,我已经在django_filters模块中发现了DateFilter和{}类。但我找不到任何与他们有关的文件。在
下面是模型、序列化程序和视图的代码。在class Holiday(models.Model):
holiday_name = models.CharField(max_length=30)
city_name = models.ForeignKey(City)
description = models.CharField(max_length=100)
date = models.DateTimeField()
def __unicode__(self):
return u'%s %s' %(self.holiday_name, self.city_name)
class HolidaySerializer(serializers.ModelSerializer):
class Meta:
model = Holiday
fields = ('holiday_name', 'city_name','description', 'date')
class HolidayFilter(django_filters.FilterSet):
class Meta:
model = Holiday
start_date = django_filters.DateFilter(name="date", lookup_type='gte')
end_date = django_filters.DateFilter(name="date", lookup_type='lte')
fields = ['date']
class HolidayList(generics.ListAPIView):
queryset = Holiday.objects.all()
serializer_class= HolidaySerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_class = HolidayFilter
我可以传递起始日期和结束日期作为参数,但它不起作用。我得到了整个查询集。在