本文介绍了DjangoFilterBackend字段= null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在所有字段中都使用了DjangoFilterBackend:
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.
像这样:
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.
我尝试过:
/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?
推荐答案
filter_fields = {'parent': ['isnull']}
这将添加一个带有'isnull'的过滤器,您可以进行如下查询:
This will add a filter with 'isnull' and you can make a query as below:
/persons/?parent__isnull=true
这篇关于DjangoFilterBackend字段= null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!