I am using django_filter along with django_rest_framework to create rest end-points for my data.
I have a model which I would like to filter by passing start_date and end_date as query parameters. So far, I have found that DateFilter
and DateRangeFilter
classes in django_filters
module. But I could not find any documentation related to them.
Here is the code for the model, serializer and the view.
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
I can pass start_date and end_date as parameters, but it is not working. I get the entire queryset.