我试图显示用户坐标与数据库中所有坐标(在这种情况下是学生宿舍)之间的距离,但是由于某种原因,获取每个条目的经度和纬度会产生错误,如标题中所示。
代码:
class SohaeDormNearbyView(generics.ListAPIView):
serializer_class = serializers.SohaeDormSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
queryset = models.SohaeDorm.objects.filter(is_active=True)
def get_queryset(self):
# Get latitude and longitude from user
lat1 = self.kwargs['user_latitude']
lon1 = self.kwargs['user_longitude']
lat2 = ExpressionWrapper(F('dorm_latitude'), output_field=FloatField())
lon2 = ExpressionWrapper(F('dorm_longitude'), output_field=FloatField())
dorm_id = ExpressionWrapper(F('id'), output_field=IntegerField())
"""
STOP! Before running, add the following stored procedure!
CREATE FUNCTION sohae_calculate_distance5(u_lat float, u_lng float,
d_id integer) RETURNS float AS $$
SELECT (6367*acos(cos(radians(u_lat))
*cos(radians(dorm_latitude))
*cos(radians(dorm_longitude)
-radians(u_lng))
+sin(radians(u_lat))*sin(radians(dorm_latitude))))
AS distance FROM sohaegeum_sohaedorm WHERE id = d_id
$$ LANGUAGE SQL;
"""
return models.SohaeDorm.objects.annotate(
# Add the distance in KM as an attribute
distance=RawSQL(
"SELECT sohae_calculate_distance5(%s, %s, %s) AS distance",
(lat1, lon1, dorm_id)
)
).filter(is_active=True)
Traceback给了我以下内容:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/api/v1/sohaegeum/dorms/nearby/8.238479/124.243347/
Django Version: 2.0.6
Python Version: 3.6.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'django.contrib.sites',
'allauth',
'allauth.account',
'rest_auth.registration',
'api',
'sohaegeum']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py" in _execute
85. return self.cursor.execute(sql, params)
The above exception (can't adapt type 'ExpressionWrapper') was the direct cause of the following exception:
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\views.py" in dispatch
483. response = self.handle_exception(exc)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\views.py" in handle_exception
443. self.raise_uncaught_exception(exc)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\views.py" in dispatch
480. response = handler(request, *args, **kwargs)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\generics.py" in get
201. return self.list(request, *args, **kwargs)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\mixins.py" in list
48. return Response(serializer.data)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py" in data
765. ret = super(ListSerializer, self).data
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py" in data
262. self._data = self.to_representation(self.instance)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py" in to_representation
683. self.child.to_representation(item) for item in iterable
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in __iter__
272. self._fetch_all()
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in _fetch_all
1179. self._result_cache = list(self._iterable_class(self))
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in __iter__
53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1068. cursor.execute(sql, params)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py" in execute
100. return super().execute(sql, params)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py" in execute
68. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py" in _execute_with_wrappers
77. return executor(sql, params, many, context)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py" in _execute
85. return self.cursor.execute(sql, params)
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\mario\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\utils.py" in _execute
85. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /api/v1/sohaegeum/dorms/nearby/8.238479/124.243347/
Exception Value: can't adapt type 'ExpressionWrapper'
即使使用F()方法获取每个宿舍条目的ID,也会导致相同的错误。
更新:我最初发布时忘记了使用错误的功能。