django - 视图集 - 过滤对象
问题描述
我在视图集中过滤对象时遇到问题...我试图仅显示字段“点”为空的对象。
我总是收到错误:NameError: name 'null' is not defined
请你帮助我好吗 ?
我的代码:
class CompanyMapSerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = ('name', 'point', 'url', 'pk')
extra_kwargs = {
'url': {'view_name': 'api:company-detail'},
}
def to_representation(self, instance):
ret = super(CompanyMapSerializer, self).to_representation(instance)
ret['point'] = {
'latitude': instance.point.x,
'longitude': instance.point.y
}
return ret
并查看设置代码:
class CompanyMapViewSet(viewsets.ModelViewSet):
queryset = Company.objects.filter(point = null)
serializer_class = CompanyMapSerializer
PageNumberPagination.page_size = 10000
请帮我。
解决方案
您没有定义 null 是什么,并且 Python 不将 null 识别为原语,您有两个选择:
queryset = Company.objects.filter(point = None) # using None
queryset = Company.objects.filter(point__isnull = True) # explicitly asking for Null
这两个查询同样有效。
推荐阅读
- c# - 根据源值数据映射到不同的值
- reporting-services - 与 SSRS 垂直对齐
- shell - 如何在 BPXBATCH 中编写长 z/OS UNIX shell 命令
- powershell - 在 Powershell 中从文本文件构造数组
- ruby-on-rails - Rails form_for 只提交最后一个输入
- r - 在 ggplot2 中创建显示平均值的分组条形图(我不想手动输入)
- c++ - 包括
不能在 Xcode 9.2 中工作 - java - Java 8 Stream比较两个对象的列表
- docker - 使用容器 IP 地址的 Docker Web 应用程序
- javascript - IntersectionObserver 并不总是在 angularjs 指令中触发