我有以下内容:
def foo(self):
print "Test"
scores = BasicScore.objects.filter(event__id=self.id).order_by('score_date')[0:1]
print scores
#return s
#return Score.objects.all().order_by('start_date')[:1]
return scores
在我的模板中:
event.foo.0.value
这将正常工作,我将能够使用 BasicScore 类。
但是以前我有:返回分数[0]
但我在日志中得到了这个:
Test
[]
还有一个例外:
Exception Type: IndexError
Exception Value:
list index out of range
Exception Location: /Library/Python/2.7/site-packages/django/db/models/query.py in __getitem__, line 207
Python Executable: /usr/bin/python
我对 Django/Python 有点陌生,但是为什么一个列表会成功返回,但访问 [0] 最终会因访问空集而出错?