2012-02-01 48 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