I get a TypeError
when I try to get data from MySQL
table and assign it to a queryset
using following code in Django ModelViewSet
def queryset(self, request):
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='password123', db='sakila')
cur = conn.cursor()
cur.execute("SELECT city_id, city, country_id FROM city")
json.dumps(list(cur))
cur.close()
conn.close()
It gives me following error
Exception Value: 'method' object is not iterable
What am I doing wrong? Any solutions? I'm kind of a noob so if you could explain the solution too, that would be great.
Traceback:
File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\rest_framework\viewsets.py" in view
87. return self.dispatch(request, *args, **kwargs)
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\rest_framework\views.py" in dispatch
466. response = self.handle_exception(exc)
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\rest_framework\views.py" in dispatch
463. response = handler(request, *args, **kwargs)
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\rest_framework\mixins.py" in list
48. return Response(serializer.data)
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\rest_framework\serializers.py" in data
674. ret = super(ListSerializer, self).data
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\rest_framework\serializers.py" in data
239. self._data = self.to_representation(self.instance)
> File "C:\Users\Naila Akbar\AppData\Local\Programs\Python\Python35-32\lib\site-packages\rest_framework\serializers.py" in to_representation
614. self.child.to_representation(item) for item in iterable
> Exception Type: TypeError at /cities/
> Exception Value: 'method' object is not iterable