如何在 drf api 响应中获取距离?

问题描述 投票:1回答:1

我有 一模一样 问题是 这个问题 (抱歉,因为有链接,没有再写完整的内容。)

另外,我已经实现了 解决办法 给出了同样的问题。

但我没有得到 distance 在API响应中。我还没有添加 distance 在序列化器字段中,所以肯定,它不会显示在响应中。但是当我把它添加到字段中时,我得到以下错误。

Field name distance is not valid for model Address.

我也试着写了一个序列化器方法字段,但不知道如何将查询参数中收到的位置传递给序列化器进行比较。

所以问题是距离应该如何在API响应中发送?

django django-rest-framework geodjango
1个回答
1
投票

如果你正在注释你的queryset用的是 distance 字段,你还需要编辑你的序列化器以包含该特定字段。

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

    class Meta:
        model = Address
        fields = ('distance', ) #add other fields you need
© www.soinside.com 2019 - 2024. All rights reserved.