Negation or exclude filter in Django REST Framework

Use the exclude parameter in filter's definition inside your filterset.

class MyFilterSet(FilterSet):
    negated_field__not = django_filters.NumberFilter(field_name='negated_field', exclude=True)

    class Meta:
        model = Model
        fields = ['some_field', 'some_other_field']

class MyViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Model.objects.all()
    serializer_class = SomeSerializer

    filter_backends = (DjangoFilterBackend,)
    filter_class = MyFilterSet

That's equivalent to Model.objects.all().exclude(negated_field__exact=value). Then, from your frontend you can exclude requesting an url like this: /api/my_viewset/?negated_field__not=value.


It sounds like you are searching for custom filter backend