类型错误: 'GeoQuerySet'对象不可调用



我正在尝试使用geodjango中的用户位置来创建Google Map API 3标记。

我在渲染Google地图和标记时遇到Internal Server Error 500。在CMD控制台上,我得到TypeError: 'GeoQuerySet' object is not callable

您可以建议解决方案并解释例外的原因吗?

views.py

class MyView(View):
    def get(self, request):
        resp = UserList.objects.all().values('nickname', 'last_location')
        print (resp)
        return JsonResponse(request, resp, safe=False)  

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from mymap_app import views as map_views
admin.autodiscover()
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^my_endpoint/$',map_views.MyView.as_view(),name='user_list'),
    url(r'^$',map_views.IndexView.as_view(), name='index'),
    ]

追溯:

Traceback (most recent call last):
  File "C:Python35-32libsite-packagesdjangocorehandlersexception.py", lin
e 41, in inner
    response = get_response(request)
  File "C:Python35-32libsite-packagesdjangocorehandlersbase.py", line 187
, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:Python35-32libsite-packagesdjangocorehandlersbase.py", line 185
, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:Python35-32libsite-packagesdjangoviewsgenericbase.py", line 68,
 in view
    return self.dispatch(request, *args, **kwargs)
  File "C:Python35-32libsite-packagesdjangoviewsgenericbase.py", line 88,
 in dispatch
    return handler(request, *args, **kwargs)
  File "C:UsersKaleabDesktopWebMapAAmap_Projectmymap_appviews.py", line 2
1, in get
    return JsonResponse(request, resp, safe=False)
  File "C:Python35-32libsite-packagesdjangohttpresponse.py", line 530, in
__init__
    data = json.dumps(data, cls=encoder, **json_dumps_params)
  File "C:Python35-32libjson__init__.py", line 237, in dumps
    **kw).encode(obj)
TypeError: 'GeoQuerySet' object is not callable
[05/Sep/2017 22:36:45] "GET /my_endpoint/ HTTP/1.1" 500 18865

看一下: data = json.dumps(data, cls=encoder, **json_dumps_params)

jsonresponse试图序列化您传递的参数,但是GeoquerySet无法通过函数调用序列化,因此可以抛出TypeError。请参阅下面的文档:https://docs.python.org/2/library/json.html

如果skipkeys为真(默认:false),则不是 基本类型(str,unicode,int,long,float,bool,none)将是 跳过而不是抚养typeserror。

为了解决这个问题,您可以做:

return HttpResponse(
    json.dumps(response, skipkeys=True), content_type="application/json"
)

也可以将GeoquerySet格式化为Python词典,然后将其传递给JSONRESPONSE。

以下更改对Views.py解决了Geoqueryset object not callable错误。

class MyView(View):
    def get(self, request):
        response = UserList.objects.all().values('nickname','last_location')
        print(response)
        return JsonResponse({'response':list(response)})

相关内容

  • 没有找到相关文章

最新更新