django-rest-framework - DjangoFilterBackend 字段 = null

标签 django-rest-framework django-filters

我将 DjangoFilterBackend 用于所有字段:

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

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

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

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

我试过:
/persons/?parent=null
/persons/?parent=Null
/persons/?parent=NULL
/persons/?parent=
/persons/?parent__isnull

在同样的简化过程中有什么建议吗?

任何需要扩展或新 View 集的建议?

最佳答案

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

这将添加一个带有“isnull”的过滤器,您可以进行如下查询:
/persons/?parent__isnull=true

关于django-rest-framework - DjangoFilterBackend 字段 = null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48319957/

相关文章:

python - 使用 Djoser、django-rest-framework-jwt 和 django-rest-framework 注册后获取 token

python - Django/Django Rest Framework - 禁用 CSRF

django - 随着执行更多插入,Django 应用程序中的数据库插入逐渐变慢

django-forms - 如何为 DateFromToRangeFilter 设置不同的占位符?

gte、lte 的 django_filters 问题

Django REST Framework——多重嵌套序列化可能吗?

python - Django,重复一个相关查询时多次命中数据库

python - 使用 django 过滤器搜索多个单词时该怎么办

python - 调用Django模板中对象的方法

python - 具有多个引用的 django 列表模型条目