django 使用sql语句查询结果返回字典Dict方法
发布时间:2020-12-15 17:14:27 所属栏目:大数据 来源:网络整理
导读:在django中一些比较复杂的方法不适合使用orm,需要使用到sql语句查询。而django默认使用sql语句查询直接返回一个列表元祖数据,使用起来及其不是很方便。 django使用sql语句方法: from?django.db?import?connectiondef?my_custom_sql(self):????with?connec
在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/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |