私は次のものを持っています:
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 に少し慣れていませんが、1 つのリストが正常に返されるのに、[0] にアクセスすると空のセットにアクセスするとエラーになるのはなぜですか?