简体   繁体   中英

Django Models Filtering

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?

list []不是一个1的列表,它是一个0的列表,并且scores[0]应该引发IndexError

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM