简体   繁体   English

Django模型过滤

[英]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. 这将正常工作,并且我将能够使用BasicScore类。

However previously I had: return scores[0] 但是以前我有:返回分数[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? 我是Django / Python的新手,但是为什么一个列表会成功返回,但是访问[0]最终会因访问空集而出错?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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