我对Django很陌生。我正在尝试将从远程postgresql数据库获取的sql数据转换为JSON,以便在react中使用它。但是在转储数据时,它会抛出错误。
`AttributeError: 'str' object has no attribute 'get'`
我尝试了许多将sql数据转储到json中的版本,例如将数据转换为列表并使用RealDictCursor,但是每个版本都会引发一个新错误。
Views.py
from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from django.http import HttpResponse
from .marketer import marketer
def marketer_list(request):
return JsonResponse(marketer)
marketer.py(获取数据并建立连接的功能)
from django.shortcuts import render, get_object_or_404
import json
import psycopg2
from psycopg2.extras import RealDictCursor
def marketer(self):
connection = psycopg2.connect(user = "db-user",
password = "*****",
host = "18.23.42.2",
port = "5432",
database = "db-name")
cursor = connection.cursor(cursor_factory = RealDictCursor)
postgreSQL_select_Query = "select id from auth_permission"
result = cursor.execute(postgreSQL_select_Query)
#print("Selecting rows from mobile table using cursor.fetchall")
#mobile = dictfetchall(result)
#items = [dict(zip([key[0] for key in cursor.description], row)) for
row in result]
return json.dumps(cursor.fetchall(), indent=2)
网址页面错误
AttributeError:'str'对象没有属性'get'
要么
在其他方法中
不可序列化