我有一个RSSFeed模型。 要获取数据库中的最后一个元素,我需要:
RSSFeed.objects.last()
# Output: <RSSFeed: www.sooperarticles.com>
我对它进行切片以获得查询中的前10个元素
first_ten_feeds = RSSFeed.objects.all()[:10]
使用first和方括号运算符是一致的:
first_ten_feeds.first()
# Output: <RSSFeed: pressetext News>
first_ten_feeds[0]
# Output: <RSSFeed: pressetext News>
但使用last和bracket运算符不一致:
first_ten_feeds[9]
# Output: <RSSFeed: FinanzNachrichten.de: Nachrichten zu IT-Dienstleistungen>
first_ten_feeds.last()
# Output: <RSSFeed: www.sooperarticles.com>
为什么?对于上面的last()和[],我希望得到相同的结果。你知道吗
RSSFeed.objects.last()
和first_ten_feeds.last()
似乎给出了相同的结果,但这对我来说没有意义。你知道吗
答案其实就在代码里。
QuerySet.last()
定义为而} clause reverted 的克隆,所以基本上不是
QuerySet.reverse()
基本上是返回queryset with the direction of the ^{SQL查询变为:
所以
first_ten_feeds.last()
将实际返回与RSSFeed.objects.last()
相同的东西。你知道吗这种行为doesn't really match the doc非常令人惊讶,更不用说完全出乎意料,我强烈建议您在django的问题跟踪程序上填写一份bug报告——要么它是预期的行为(至少对于django开发人员而言),然后应该清楚地记录它,要么它是一个普通的bug。你知道吗
相关问题 更多 >
编程相关推荐