0

我在视图集中过滤对象时遇到问题...我试图仅显示字段“点”为空的对象。

我总是收到错误: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

请帮我。

4

1 回答 1

0

您没有定义 null 是什么,并且 Python 不将 null 识别为原语,您有两个选择:

queryset = Company.objects.filter(point = None) # using None
queryset = Company.objects.filter(point__isnull = True) # explicitly asking for Null

这两个查询同样有效。

于 2018-07-08T22:26:50.440 回答