Python 四处走动
我的代码如下:Python 四处走动,python,django,list,exception,Python,Django,List,Exception,我的代码如下: for p in qs: set = None try: set = p.property.property_locations.all() except IndexError: pass if set: 问题是,当set为none时,它仍然会从django.db.models.query的这部分抛出indexer-ror: try: qs = self._clone() qs.query.set
for p in qs:
set = None
try:
set = p.property.property_locations.all()
except IndexError:
pass
if set:
问题是,当set为none时,它仍然会从django.db.models.query的这部分抛出indexer-ror:
try:
qs = self._clone()
qs.query.set_limits(k, k + 1)
return list(qs)[0]
except self.model.DoesNotExist, e:
raise IndexError(e.args)
如何阻止系统抛出此错误并继续执行for循环中的下一个元素?在任何情况下,代码中都有两个错误:
try:
[code that might raise an exception]
except Error:
[code that handles the exception]
else:
[code that executes when no exception was raised]
(当然,用错误代替实际异常)
这样,您甚至不必在该点检查“set”。将set重命名为其他名称时是否有效?set是一个内置函数,“当set为none时”?您正在将
set
设置为None
。它必须始终为None
。你的问题是什么?你到底有什么问题?将QuerySet
对象分配给set
永远不会引发索引器。你能说得更精确一点吗?原来错误是由一些.pyc文件没有更新引起的。@moutone:原来你应该结束这个问题。如果代码与实际问题(或实际解决方案)无关,最好删除该问题。