1

I have exactly the same problem as this SO question (Sorry for the link and not writing the whole content again.)

Also, I have already implemented the solution given for the same problem.

but I am not getting distance in API response. I haven't added distance in serializer fields so definitely, it won't show up in response. But when I add it to fields I get the following error.

Field name distance is not valid for model Address.

I also tried to write a serializer method field but was not sure how to pass the location received in query params to the serializer for comparison.

So the question is how the distance is supposed to send in API response?

Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61

1 Answers1

1

If you are annotating your queryset with the distance field, you also need to edit your serializer to include that specific field:

class AddressSerializer(serializers.ModelSerializer):
    #...
    distance = serializers.DecimalField(max_digits=10, decimal_places=2)
    #...

    class Meta:
        model = Address
        fields = ('distance', ) #add other fields you need
drec4s
  • 7,946
  • 8
  • 33
  • 54
  • Thanks! You are correct. But I had to use `CharField` as the `m` was getting added to the value. Anyway, can you share the link to a document, where such a case is documented? – Kishor Pawar May 11 '20 at 17:41