【发布时间】:2011-06-10 23:06:30
【问题描述】:
我正在编写一个非常简单的应用程序来存储足球比赛的结果,但我遇到了以下问题。运行其中一个单元测试时,以下代码:
listCompetition = Competition.objects.filter(compId=competitionId)
if len(listCompetition) == 0:
#some code here
else:
#some code here
给出以下错误:
File "C:\Users\admin\workspace\project\src\bla\bla\module.py", line 222, in getMatches
if len(listCompetition) == 0:
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 82, in __len__
self._result_cache = list(self.iterator())
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 286, in iterator
obj = model(*row[index_start:aggregate_start])
TypeError: __init__() takes exactly 3 arguments (4 given)
但是,如果我将第一行代码替换为:
listCompetition = list(Competition.objects.filter(compId=competitionId))
然后它工作得很好。为什么它会以这种奇怪的方式表现?如果我在 Competition 类的构造函数中只定义了两个,那么 Django 怎么会传递 4 个参数?如果有帮助,这里是竞赛类的模型定义:
class Competicion(MultiName):
def __init__(self, canonicalName, compId):
super(Competition, self).__init__(canonicalName, compId)
class MultiName(models.Model):
entId = models.CharField(null=True, max_length=25);
canonicalName = models.CharField(max_length=50, primary_key=True);
def __init__(self, canonicalName, entId=None):
super(MultiName, self).__init__()
self.canonicalName = canonicalName;
self.entId = entId;
非常感谢。
【问题讨论】: