+ -
当前位置:首页 → 问答吧 → django orm和sql查询转换

django orm和sql查询转换

时间:2011-02-17

来源:互联网

在django中
有什么方法 可以把用sql语句 得到的结果:一个tuple,转换成django.db.models.query.QuerySet,这中类型吗?就是直接用django的orm从数据库中取到的数据 
如 

>>>node=cartree.objects.filter(parentid=-100)
>>> node
[<cartree: -100 监控中心 -100>, <cartree: 1 汇博 -100>]
>>> type(node)
<class 'django.db.models.query.QuerySet'>

>>> n=cursor.execute("select a.nodeid,a.nodename,a.userphone,a.username,'true',b.locate,b.speed,b.direction,b.status,b.gps_time from carinfo a join carlast b where a.equid=b.equid;")
>>> nodeinfo=cursor.fetchall()
>>> nodeinfo
((1000L, '\xe8\xb1\xabEB7822', 2147483647L, '\xe7\x87\x95\xe8\x8e\x8e', 'true', '36.060108,114.201501', 0, 251, 'fffffbff', datetime.datetime(2011, 2, 17, 13, 24, 48)),)
>>> type(nodeinfo)
<type 'tuple'>


有什么方法 可以吧下面这中方式返回的数据 转换为上面那种格式的吗?

作者: nothingsss   发布时间: 2011-02-17

那为什么不用ORM?

作者: iambic   发布时间: 2011-02-17

orm怎么实现join查询阿,我不知道,orm可以实现复杂的查询吗?

作者: nothingsss   发布时间: 2011-02-17

n=cursor.execute("select a.nodeid,a.nodename,a.userphone,a.username,'true',b.locate,b.speed,b.direction,b.status,b.gps_time from carinfo a join carlast b where a.equid=b.equid;")

就上面这个语句,字段,语句里就有,你看看怎么实现orm查询,本人在摸索中谢谢指点

作者: nothingsss   发布时间: 2011-02-17