In my views.py file :
cursor_cloud = connections['cloud'].cursor()
def get_regions(request):
if request.method == 'GET':
query = """
SELECT DISTINCT `Region` AS `Region`
FROM `aws`
WHERE `flag` = '1'
LIMIT 0, 100
"""
with cursor_cloud as cursor:
cursor.execute(query)
columns = cursor.fetchall()
column = []
for col in columns:
column.append(col[0])
json_obj = json.dumps(column)
result = HttpResponse(json_obj, 'application/json', charset='utf-8')
# cursor.close()
return result
Oddest thing ever : first time, the URL http://localhost:8000/api/regions works giving a proper JSON, after which it fails saying that cursor
was closed. To make it work again I had to restart the server.
Finally, I had to remove with cursor_cloud as cursor
and make everything with cursor_cloud
like cursor_cloud.execute(query)
to make it work always.
Why is with cursor_cloud as cursor
acting only once ?