Search code examples
djangodjango-rest-viewsets

View Set - filter objects


I have a problem with filtering objects in View Set... I am trying to show objects only where field 'point' is null.

I always get error: NameError: name 'null' is not defined

Could you please HELP ME ?

My code:

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

And view set code:

class CompanyMapViewSet(viewsets.ModelViewSet):
    queryset = Company.objects.filter(point = null)
    serializer_class = CompanyMapSerializer
    PageNumberPagination.page_size = 10000

Please help me.


Solution

  • You are not defining what null is, and Python doesn't recognize null as a primitive, you've got two options:

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

    These two queries are equally valid.