微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

django 使用sql语句查询结果返回字典Dict方法

在django中一些比较复杂的方法不适合使用orm,需要使用到sql语句查询。而django认使用sql语句查询直接返回一个列表元祖数据,使用起来及其不是很方便。

django使用sql语句方法

from django.db import connection

def my_custom_sql(self):
    with connection.cursor() as cursor:
        cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
        cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
        row = cursor.fetchone()

    return row

认情况下,Python DB API将返回没有字段名称的结果,意味着您最后得到一个list值,而不是一个dict。在消耗较小的性能和内存成本下,可以使用以下内容返回结果dict。

第一种 方法

def dictfetchall(cursor):
    "从cursor获取所有行数据转换成一个字典"
    columns = [col[0] for col in cursor.description]
    return [
        dict(zip(columns, row))
        for row in cursor.fetchall()
    ]

第二种方法:是使用python标准库:collections.namedtuple()。

namedtuple是一个类似元组的对象,其字段可通过属性查找访问; 它也是可索引和可迭代的。但是不可修改

from collections import namedtuple

def namedtuplefetchall(cursor):
    "Return all rows from a cursor as a namedtuple"
    "从cursor获取所有行数据转换成一个namedtuple数据类型"
    desc = cursor.description
    nt_result = namedtuple('Result', [col[0] for col in desc])
    return [nt_result(*row) for row in cursor.fetchall()]

以下三者数据访问

认情况
>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> cursor.fetchall()
((54360982, None), (54360880, None))

# 转换字典
>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> dictfetchall(cursor)
[{'parent_id': None, 'id': 54360982}, {'parent_id': None, 'id': 54360880}]

# 转换为nametuple数据类型
>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> results = namedtuplefetchall(cursor)
>>> results
[Result(id=54360982, parent_id=None), Result(id=54360880, parent_id=None)]
>>> results[0].id
54360982
>>> results[0][0]
54360982

django官网文档:https://docs.djangoproject.com/el/2.1/topics/db/sql/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐