DjangoFilterBackend 字段 = null

2024-02-11

我将 DjangoFilterBackend 与所有字段一起使用:

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

它非常适合通过具有固定值或布尔值的一个或多个字段的 url 进行查询。

像这样:

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

但我还需要过滤具有空值的字段,但它不起作用。

I tried:

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

在这个相同的简化过程中有什么建议吗?

有任何需要扩展或新视图集的建议吗?


除了 isnull 之外,您可能还想查询“精确”值。在这种情况下,

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

您可以使用 '=True' 进行查询,

/persons/?parent__isnull=True
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DjangoFilterBackend 字段 = null 的相关文章

随机推荐