django - how to get distance in drf api response?
问题描述
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?
解决方案
如果您使用该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
推荐阅读
- ethereum - 具有以下条件的以太坊彩票智能合约
- django - 在我所有的 django URL 中都有一个常量参数
- go - 地图未更新:地图值是固定大小的数组
- vb6 - Visual Basic 6 - 参数不是可选的
- android -
使用 CDATA 和标签在 android 字符串文件中添加多个换行符? - pine-script - 在不使用 ema() 的情况下正确计算当前 EMA 值
- authentication - 如何在laravel 8登录期间检查其他字段
- typo3 - TYPO3 为 IRRE tt_content 隐藏 colPos
- javascript - 反应过渡组幻灯片动画在我的 Slider 组件上不起作用
- python - 获取当月每一天的日期值