Django将原始查询结果作为对象获取

时间:2015-03-04 10:47:45

标签: mysql sql django tuples

我最近开始学习Django。我有2个案子。其中一个按我的意愿工作。另一个不是。

工作案例:

cursor = connections['ticket'].cursor()
        raw_query = "SELECT * FROM ticket WHERE customer_code = '%s'" % customer.custid
        cursor.execute(raw_query)
        tickets = cursor.fetchall()

当我在模板中使用门票时:

{{ ticket.name }}

打印其值。

现在其他不是工作案例:

cursor = connections['ubds'].cursor()
        raw_query = """SELECT  * FROM IncomeTransactions WHERE CustID = '%s' AND TranType = '17'"""\
                    """ORDER BY TranDate ASC""" % (request.session['member_id'])
        cursor.execute(raw_query)
        invoices = cursor.fetchall()

当我在print中打印它的值时:

{{ invoice.name }} #blank result

它不打印任何东西。只是空白。但是,当我访问索引并打印它时,

{{ invoice.2 }} #it printing 3rd column from table

我怀疑第一个返回对象,但第二个返回数组(元组)。但他们都使用相同的功能。可能是什么原因?如何在第二种情况下通过列名访问?

编辑:在两种情况下打印结果

print tickets

[(917235,162,None,datetime.datetime(2014,1,17,0,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043f \ u0435 \ u0440 \ u0442 \ u0444 \ u0441 \ u0425 \ u0425 \ u0401',u' 0002',无,无,8199484.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u' 192.168.0.101',u' mijiddorj',29522,19,3,无,无,无),(920746,681,无,datetime.datetime(2014,2,17,0,0,tzinfo =),u' 0001-08& #39;,u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0444 \ u0441 \ u0425 \ u0425 \ u041a',u' 0002&# 39;,无,无,5237942.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u&# 39; 192.168.0.141',u' mijiddorj',29522,19,3,无,无,无),(924625,1148,无,datetime.datetime(2014,3,17,0) ,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0 440 \ u0442 \ u0444 \ u0444 \ u0425 \ u402',u' 0002',无,无,6574669.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u' 192.168.0.141',u' mijiddorj',29522,19,3,无,无,无),(928477,1690,无,datetime.datetime(2014,4,17,0,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043f \ u0435 \ u0440 \ u0442 \ u0444 \ u0444 \ u0425 \ u0425 \ u0441 \ u 392,u' 0002',无,无,4269964.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u' 192.168.0.31',u&#39 ; mijiddorj',29522,19,3,无,无,无),(932106,1103,无,datetime.datetime(2014,5,19,0,0,tzinfo =),u' 0001- 04',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0444 \ u0441 \ u0425 \ u0425 \ u041a',u' 0002& #39;,无,无,2408917.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d ',17,无,无,无,无,u' 192.168.0.121',u' mijiddorj',29522,19,3,无,无,无),( 934461,173,无,datetime.datetime(2014,6,20,0,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0444 \ u0444 \ u0441 \ u0444 \ u0425 \ u041a',u' 0002',无,无,12024.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u' 192.168.0.109',u' mijiddorj', 29522,19,3,无,无,无),(936002,8,无,datetime.datetime(2014,7,17​​,0,0,tzinfo =),u' 0001-04',u& #39; \ u0410 \ u0433 \ u0444 \ u044f \ u043f \ u0440 \ u043 \ u043f \ u0435 \ u0440 \ u0442 \ u0444 \ u0441 \ u0425 \ u0425 \ u0401',u' 0002',无,无,52355.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u' 192.168.0.75 ',u' mijiddorj',29522,19,3,无,无,无),(938032,16,无,datetime.datetime(2014,8,2) 0,0,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0438 \ u0441 \ u0425 \ u0425 \ u041a',u' 0002',无,无,40833.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d ',17,无,无,无,无,u' 192.168.0.78',u' mijiddorj',29522,19,3,无,无,无),( 940140,53,无,datetime.datetime(2014,9,10,0,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0444 \ u0444 \ u0441 \ u0444 \ u0425 \ u041a',u' 0002',无,无,1399746.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u' 192.168.0.143',u' mijiddorj', 29522,19,3,无,无,无),(942116,179,无,datetime.datetime(2014,9,17,0,0,tzinfo =),u' 0001-08',u& #39; \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0444 \ u0441 \ u0425 \ u0425 \ u041a',u' 0002 ',无,无,40833.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,你' 192.168.0.54',你' mijiddorj',29522,19,3,无,无,无),(946440,96,无,datetime.datetime(2014,10,17) ,0,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0438 \ u0441 \ u0425 \ u0425 \ u041a',u' 0002',无,无,270453.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d& #39;,17,无,无,无,无,u' 192.168.0.196',u' mijiddorj',29522,19,3,无,无,无),(949055 ,105,无,datetime.datetime(2014,11,13,0,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0444 \ u0444 \ u0441 \ u0425 \ u0425 \ u041a',u' 0002',无,无,2869320.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u' 192.168.0.196', u' mijiddorj',29522,19,3,无,无,无),(952901,169,无,datetime.datetime(2014,12,17,0,0,tzinfo =),u&#39 ; 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0438 \ u0441 \ u0425 \ u0425 \ u041a',u&# 39; 0002',无,无,7107883.0,u' \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,你' 192.168.0.196',你' mijiddorj',29522,19,3,无,无,无),(1118349,177,无,datetime.datetime(2015,1) ,19,0,0,tzinfo =),u&#; 0001-08',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0438 \ u0441 \ u0425 \ u0425 \ u041a',你' 0002',无,无,7546811.0,你&u 394 u044d',17,无,无,无,无,u' 192.168.0.63',u' Handaa',29522,19,3,无,无,无), (1122726,273,无,datetime.datetime(2015,2,17,0,0,tzinfo =),u' 0001-08',u' \ u0410 \ u0 437 \ u0444 \ u044f \ u043f \ u0440 \ u044f \ u0435 \ u0440 \ u0442 \ u0444 \ u0444 \ u0425 \ u0425 \ u0401',u' 0002',无,无,8005173.0,u&# 39; \ u0414 \ u0443 \ u043b \ u0430 \ u0430 \ u043d \ u044b \ u04af \ u043d \ u044d',17,无,无,无,无,u' 192.168.0.75',u& #39; Handaa',29522,19,3,无,无,无)]

print invoices

((1L,u' T15021715135295',2L,u' call',u' \ u0428 \ u0443 \ u0444 \ u0434 \ u0445 \ u0440 \ u0440 \ u0438 \ u0443 \ u043b \ u0441 \ u0430 \ u043d.test122123',u'关闭',u' 99881122',2L,u' \ u0414 \ u04af \ u0433 \ u044d \ u0440 \ u0445 \ u04af \ u04af \ u042d \ u043d \ u0445 \ u0444 \ u043c \ u044d \ u0433',4L,u' \ u0414 \ u0430 \ u0432 \ u0430 \ u0430 \ u0445 \ u04af \ u04af \ u0410 \ u043b \ u0442 \ u0430 \ u043d \ u043d \ u0430 \ u0432 \ u0447',10L,u' \ u0410 \ u0432 \ u0442 \ u043e \ u0430 \ u0430 \ u0430 \ u0445 \ u0443 \ u0439 \ u043d \ u0430 \ u043b \ u0431 \ u0430',datetime.datetime(2015,2,21,15,13,​​52,tzinfo =),datetime.datetime(2015,2,26,18,53,36,tzinfo =),datetime.datetime (2015,2,26,17,20,tzinfo =),datetime.datetime(2015,2,17,15,13,​​52,tzinfo =),无,无,无,u' agent-101-1424234875 -330.wav',u' 0002',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0438 \ u0441 \ u0425 \ u0425 \ u0425 \ u041a',你'',没有,你',你'',你'',你',你',' \ x01',' \ x00',' \ x01',&# 39; \ x01'),(2L,u' T15030212560836',2L,u'呼叫',u'测试测试',u'打开' ,u' 99081133',2L,u' \ u0414 \ u04af \ u0433 \ u044d \ u0440 \ u0445 \ u04af \ u04af \ u042d \ u043d \ u0445 \ u0447 \ u0438 \ u043c \ u044d \ u0433&#39 ;,无,无,无,无,datetime.datetime(2015,3,2,12,56,8,tzinfo =),datetime.datetime(2015,3,2,12,56,8,tzinfo =),无,无,你',无,无,无,你' 0002',u' \ u0410 \ u0437 \ u0438 \ u044f \ u043f \ u0440 \ u043e \ u043f \ u0435 \ u0440 \ u0442 \ u0444 \ u0441 \ u0425 \ u0425 \ u041a',u' \ u0411 \ u043e \ u043b \ u0434',14000L,u' 3',u' \ u0410 \ u043c \ u0430 \ u0440',u' 55',u'',u' boldoo@gmail.com',' \ x00&#39 ;,' \ x00',' \ x00',' \ x00'))

差异是第一个(工作一个)有[()],但第二个(不工作)有(())。

2 个答案:

答案 0 :(得分:1)

嗯,我不明白为什么第一个例子有效。它不应该 - cursor.fetchall()总是返回元组列表。

但是如果你想获得dicts而不是元组的列表,那么请使用django documentation中的食谱:

def dictfetchall(cursor):
    desc = cursor.description
    return [dict(zip([col[0] for col in desc], row))
              for row in cursor.fetchall()]
...
cursor.execute(raw_query)
invoices = dictfetchall(cursor)

答案 1 :(得分:1)

要将原始结果作为模型实例获取,请使用raw() queryset方法:

raw_query = "SELECT * FROM ticket WHERE customer_code = '%s'"
tickets = Ticket.objects.raw(raw_query, params=[customer.custid])

始终(是的,总是)使用params参数传递参数,这将保护您免受SQL注入攻击。将参数传递给cursor.execute时,您应该这样做。