当我删除便利api时,出现的问题是TypeError: 'int' object is not iterable
并指向此行id, = args[0]
...我将如何解决这个问题?我指定args [0]的问题是否与此Stacks问题类似?
CODE:
object.py (父类)
def retrieve_assessment_result(self, *args):
id, owner, assessment = None, None, None
if len(args) == 1:
id, = args[0]
elif len(args) == 2:
owner, assessment = args
print 'testa', owner, assessment, id
else:
raise ValueError('Value being passed is an object')
if id is not None:
print 'testi', id
return self.session.query(Assessment_Result).\
filter(Assessment_Result.id == id).one()
elif owner is not None:
print 'testo', owner
return self.session.query(Assessment_Result).\
filter(Assessment_Result.owner == owner).one()
elif assessment is not None:
print 'testa', assessment
return self.session.query(Assessment_Result).\
filter(Assessment_Result.assessment == assessment).one()
convenience.py (子类)
def retrieve_assessment_result(self, username, name):
owner = self.retrieve_user(username)
assessment = self.retrieve_assessment(name)
return super(ConvenienceAPI, self).retrieve_assessment_result(owner, assessment)
问题2的回溯:
(当我删除子类以单独检查ID
时)
...
File "/usr/local/lib/python2.7/site-packages/pyramid-1.5.7-py2.7.egg/pyramid/config/views.py", line 501, in _requestonly_view
response = view(request)
File "/Users/ack/code/venv/NotssWEB/notssweb/views/views.py", line 368, in get_assessment_result
assessment_result = api.retrieve_assessment_result(assessment_result_id)
File "/usr/local/lib/python2.7/site-packages/notssdb/api/object.py", line 313, in retrieve_assessment_result
id, = args[0] # <-- this the issue? see object.py above
TypeError: 'int' object is not iterable
答案 0 :(得分:1)
您正在使用元组分配;您正尝试将一个整数分配给目标的元组。即使只有一个目标,也需要右手表达来生成具有相同数量元素的序列。
删除逗号,删除[0]
切片,或在右侧添加逗号以使其成为元组:
id = args[0]
或
id, = args
或
id, = args[0],
但后一个选项是第一个选项的效率相当低的版本。