我现在有类似这样的需求:用户传入经纬度坐标,对“店面”表(每一个表项都使用 GeoDjango 的 PointField 存储经纬度)进行排序(用户的距离由近到远排序),并且使用 DRF 的 CursorPagination 进行分页加载。 问题出在分页加载时,如果传入了 cursor 参数就会报错误。代码如下: views.py:
class BranchesView(ListAPIView): authentication_classes = (MemberAuthentication,)
serializer_class = serializers.BranchesSerializer
pagination_class = BranchesCursorPagination
def get_queryset(self):
if self.request.user.member_of:
raise exceptions.RequestFailed
try:
latitude = float(self.request.query_params['latitude'])
longitude = float(self.request.query_params['longitude'])
assert 90.00 >= latitude >= -90.00
assert 180.00 >= longitude >= -180.00
user_coordinates = Point(longitude, latitude, srid=4326)
except:
raise exceptions.ArgumentError
return Branch.objects.get_existing_all().annotate(distance=Distance('location__coordinates', user_coordinates))
分页器:
class BranchesCursorPagination(CursorPagination): page_size = 20 ordering = 'distance'
def get_paginated_response(self, data):
#这个是为了改变输出的格式
response = {
'data': {
'links': {
'next': self.get_next_link(),
'previous': self.get_previous_link(),
},
'branches': data,
},
'code': 0,
'error': '',
}
return Response(response)
然后访问带有 cursor 参数的链接时返回了以下错误: could not convert string to float: '1190493.2321520457 m'
请问一下站友们有没有碰到这种情况?或者说这种情况应该如何解决(或者是有别的分页策略)