TypeError: 'GeoQuerySet' object is not callable

1.4k views Asked by At

I am trying to create google map API 3 markers using users' locations in Geodjango.

I encounter Internal Server Error 500 in rendering the google map and markers. On the CMD console , I get TypeError: 'GeoQuerySet' object is not callable.

Can you please suggest a solution and explain the cause for the exception?

The 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)  

The 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:

Traceback (most recent call last):
  File "C:\Python35-32\lib\site-packages\django\core\handlers\exception.py", lin
e 41, in inner
    response = get_response(request)
  File "C:\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187
, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185
, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Python35-32\lib\site-packages\django\views\generic\base.py", line 68,
 in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Python35-32\lib\site-packages\django\views\generic\base.py", line 88,
 in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\Kaleab\Desktop\WebMap\AAmap_Project\mymap_app\views.py", line 2
1, in get
    return JsonResponse(request, resp, safe=False)
  File "C:\Python35-32\lib\site-packages\django\http\response.py", line 530, in
__init__
    data = json.dumps(data, cls=encoder, **json_dumps_params)
  File "C:\Python35-32\lib\json\__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
2

There are 2 answers

0
E_K On

Look at this: data = json.dumps(data, cls=encoder, **json_dumps_params).

JsonResponse is trying to serialize the parameters you passed it, but a GeoQuerySet is not serializable by the function call and therefore throws a TypeError. See the documentation below: https://docs.python.org/2/library/json.html

If skipkeys is true (default: False), then dictionary keys that are not of a basic type (str, unicode, int, long, float, bool, None) will be skipped instead of raising a TypeError.

In order to get around this you could instead do:

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

It would also be possible to format the GeoQuerySet as a python dictionary and then pass it to JsonResponse.

0
Kaleab Woldemariam On

The following change to the views.py resolved the Geoqueryset object not callable error.

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