2011-07-07 28 views
0

我想傳遞的元組這個功能,但我得到的錯誤如何通過元組作爲參數傳遞給Django的功能

mytuple = [('id', 'name','author')] 
mybooks = Book.objects.values_list(mytuple) 

誤差

'list' object has no attribute 'split' 
+0

你沒有將一個元組傳遞給你的函數,而是一個列表。 –

+0

其實我是使用元組,但是當我得到錯誤,我把[]測試 –

回答

7

你應該使用:

mytuple = ('id', 'name', 'author') 
mybooks = Book.objects.values_list(*mytuple) 
+1

謝謝,這工作,但你能請解釋如何? –

+0

它的工作原理是因爲他在擴展它的元組名稱前使用了*。 –

+1

在Python中,調用'foo(* list)'相當於調用'foo(list [0],list [1],...,list [len(list)-1])''。 –

相關問題