django-rest-framework filter by date=None

Running django-filter==1.0.4 and djangorestframework==3.6.3.

The correct answer didn't work because module paths have changed in django-filter (BooleanFilter moved to django_filters.rest_framework).

And the __isnull didn't work either, I had to use lookup_expr:

from django_filters import rest_framework as filters


class WidgetFilter(filters.FilterSet):
    no_date = filters.BooleanFilter(name='date', lookup_expr='isnull')

    class Meta:

        model = Widget
        fields = ( 'date')

Got the answer from https://github.com/carltongibson/django-filter/issues/743

Now I can filter by "no date" using http://localhost:8000/widgets/?no_date=True


I ran into a similar situation today, and it appears as if out of the box django rest framework* supports this filter as:

~/your_endpoint/?date__isnull=True

This matches up with how the the equivalent ORM query would look. If that's ugly, you can use the docs example to transform that query parameter to something else without having to override get_queryset

  • I'm using 2.4.3, but I don't believe this is a new thing

Specifying isnull directly in the filter's name argument as 'date__isnull' seems to be working for me with Django REST Framework 3.1.3.

class WidgetFilter(django_filters.FilterSet):
    date = django_filters.DateTimeFilter(name='date')
    no_date = django_filters.BooleanFilter(name='date__isnull')
    class Meta:
        model = Widget
        fields = []

In case there is no more elegant way, here is how I bodged my workaround, but I'd still like to know of a solution that uses django-filter, if one exists.

class WidgetSet(viewsets.ModelViewSet):
    model = Widget

    def get_queryset(self):
        if 'no_date' in self.request.QUERY_PARAMS:
            return self.model.objects.filter(date=None)
        return self.model.objects.all()