Reputation: 398
I have the following:
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
And in my Template:
event.foo.0.value
This will work fine and I will be able to use BasicScore class.
However previously I had: return scores[0]
But I got this in the logs:
Test
[]
And the exception:
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
I'm a little new to Django/Python, but why would the list of one return successfully, but accessing [0] end up erroring with accessing an empty set?
Upvotes: 2
Views: 279
Reputation: 375932
The list []
is not a list of one, it is a list of zero, and scores[0]
should raise an IndexError
.
Upvotes: 1