2013-05-14 110 views
-3

我應用此代碼從表中獲取id。從django數組中獲取特定值

 getItemid=cursor.execute("Select id from shop_carthistory where order_id =%s",[order.order_number]) 
    row=cursor.fetchall() 
    context = {order_id": row } 


{{order_id}} gives => ((37L,),) 

我只需要37出來的

+8

你爲什麼要使用原始SQL,而不是ORM?如果你只需要一個結果,你爲什麼使用'fetchall()'? – 2013-05-14 12:11:16

+1

...什麼是你的問題? – 2013-05-14 12:30:06

回答

1

你必須用與一個元素的元組一個元素的元組:

In [35]: a = ((37,),) 

In [36]: a 
Out[36]: ((37,),) 

In [37]: a[0] 
Out[37]: (37,) 

In [38]: a[0][0] 
Out[38]: 37 
+0

謝謝了。 – 2013-05-21 12:26:05