Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DjangoFilterBackend field = null

Tags:

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?

like image 370
Neumann Avatar asked Jan 18 '18 11:01

Neumann


1 Answers

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
like image 75
Kenneth.Wong Avatar answered Sep 19 '22 12:09

Kenneth.Wong