【问题标题】:Doesn't calling Entry.object.all() defeat the point of Django endless pagination?调用 Entry.object.all() 不会破坏 Django 无休止的分页吗?
【发布时间】:2016-01-13 10:37:15
【问题描述】:

https://django-endless-pagination.readthedocs.org/en/latest/twitter_pagination.html 上,我了解到views.py 的示例可能如下所示:

from endless_pagination.decorators import page_template

@page_template('myapp/entry_index_page.html')  # just add this decorator
def entry_index(
        request, template='myapp/entry_index.html', extra_context=None):
    context = {
        'entries': Entry.objects.all(),
    }
    if extra_context is not None:
        context.update(extra_context)
    return render_to_response(
        template, context, context_instance=RequestContext(request))

这似乎表明我们必须调用Entry.objects.all() 并将结果传递给模板。但是Entry.objects.all() 不是已经发出查询调用来检索所有相应的数据库对象,从而违背了分页的主要目标之一(一次检索小块数据)吗?

【问题讨论】:

  • 查询集是惰性的。我不知道这个是什么时候评估的(因此不能提供一个可靠的答案),但是在使用.all() 之后可以继续.filter()

标签: python django pagination django-endless-pagination


【解决方案1】:

Django 中的查询是惰性的,这意味着 Entry.objects.all() 不会带来完整的 Entries 列表,它只是指定 Endless 将显示的结果的范围。

【讨论】:

  • Entry.objects.all()[0:10] 会执行查询吗?
  • @personjerry 是的,Entry.objects.all()[0:10] 将使用 LIMIT 在 SQL 上执行查询。
猜你喜欢
  • 1970-01-01
  • 2013-06-28
  • 2013-05-24
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多