10

I used DjangoFilterBackend with all fields:

class EntitiesViewSet(viewsets.ModelViewSet):
    queryset = Entity.objects.all()
    serializer_class = EntitiesSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = (DjangoFilterBackend,)
    filter_fields = '__all__'

It worked perfectly for querying through the url of one or more fields with fixed or boolean values.

like this:

http://localhost:8000/api/persons/?news_by_email=True
http://localhost:8000/api/persons/?issuer=SSP-SC

But I need to also filter for fields with null value and it is not working.

I tried:

/persons/?parent=null
/persons/?parent=Null
/persons/?parent=NULL
/persons/?parent=
/persons/?parent__isnull

Any suggestions in this same simplified process?

Any suggestions that require an extension or new viewset?

1
  • what about /persons/?parent=None?
    – marke
    Commented Mar 8, 2019 at 16:23

2 Answers 2

7

You may also want to query the 'exact' value, besides isnull. In that case,

filter_fields = {'parent': ['exact', 'isnull']}

And you can make a query with '=True',

/persons/?parent__isnull=True
3
filter_fields = {'parent': ['isnull']}

This will add a filter with 'isnull' and you can make a query as below:

/persons/?parent__isnull=true
1
  • 2
    Note that filter_fields is now filterset_fields but yes, this works fine. Add [ 'exact', 'isnull' ] if you also want to be able to filter on the underlying exact value.
    – BjornW
    Commented Apr 16, 2020 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.