我是python的新手,我正在研究models.py
,我可以看到它向我显示数据为元组,我需要关联数组,任何人都可以帮我吗,这是我的代码,它向我展示了输出这个输出((1, "What's up?", datetime.datetime(2019, 4, 19, 7, 38, 6, 449735))
,我需要字段值数据
import datetime
from django.utils import timezone
from django.db import connection
from django.db import models
class Question():
@classmethod
def get_poll_question(cls):
with connection.cursor() as cursor:
db_table = "polls_question"
cursor.execute('SELECT * FROM '+db_table)
allquestion = cursor.fetchall()
return allquestion
你可以
试试这个
def dictfetchall(cursor):
"Return all rows from a cursor as a dict"
columns = [col[0] for col in cursor.description]
return [
dict(zip(columns, row))
for row in cursor.fetchall()
]
class Question():
@classmethod
def get_poll_question(cls):
with connection.cursor() as cursor:
db_table = "polls_question"
cursor.execute('SELECT * FROM '+db_table)
allquestion = dictfetchall(cursor)
return allquestion