查看集 - 筛选对象
View Set - filter objects
我在视图集中过滤对象时遇到问题...我试图仅显示字段 'point' 为空的对象。
我总是收到错误: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
这两个查询同样有效。
我在视图集中过滤对象时遇到问题...我试图仅显示字段 'point' 为空的对象。
我总是收到错误: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
这两个查询同样有效。