2012-02-01 34 views
0

我不能確定問題出在哪裏,可能是語法,或者有關於django返回的查詢集的東西,我不太明白。有覆蓋__add__運算符返回元組的問題python/django

Class1(models.Model): 
    ... 
    def __add__(self,other) 
     return other + ({'attribute': value}, ..) 

我希望類之間的增加將返回1元組與所有的對象,並將其放入它。 因此,我不得不避免sum()函數,因爲它正在尋找整數。

Class Summate(): 
    @staticmethod 
    def sum_tuples(items) 
     return reduce(lambda y,x: x+y, items) 


eg_list = Class1.objects.all() 
values = Summate.sum_tuples(eg_list) 

我得到一個TypeError:reduce()沒有初始值的空序列。

UPDATE: My lists were empty. I resolved that and received this error

TypeError: unsupported operand type(s) for +: 'dict' and 'dict' But shouldn't it be appending the tuple and not the dicts?

有什麼想法?我是否完全錯誤?

謝謝,

+0

現在'eg_list'不是'QuerySet'對象嗎?你應該使用'list(eg_list)' – cha0site 2012-02-01 18:34:43

+0

@ user334796使它成爲一個真正的列表,如果你得到這個錯誤,我只能想象你沒有粘貼字典後的逗號? – 2012-02-01 18:52:49

回答

2

您的過濾器必須簡單地返回沒有結果。我只是測試你的代碼,它的工作原理。

+0

你是對的代碼是好的,列表是空的,字典錯誤是通過在尾隨字典後附加','來解決的。謝謝Yuji,我在某個時間點瘋了= P – 2012-02-01 18:52:48