Django lazy QuerySet和分页 [英] Django lazy QuerySet and pagination
问题描述
我阅读了这里 Django查询语言是懒惰的,它在实际打印之前不会被评估。我使用django的内置分页做了简单的分页。我没有意识到有些应用程序已经有django-pagination和django-endless这样的工作。
无论我是否猜测QuerySet当我这样做时,我仍然很懒惰
entries = Entry.objects.filter(...)
paginator = Paginator(entries,10)
output = paginator.page(page)
return HttpResponse(output)
每当我想要获取我目前要查看的页面时,这部分被调用。
我需要知道,因为我不知道
如果你想看看发生的地方,导入 django .db.connection
并检查查询
>>> from django.db import connection
>>>>来自django.core.paginator import Paginator
>>>> queryset = Entry.objects.all()
让我们创建分页符,看看是否有任何查询发生:
>>> paginator = Paginator(queryset,10)
>>> print connection.queries
[]
还没有。
>>> page = paginator.page(4)
>>> page
<第4页,共788页>
>>> print connection.queries
[{'time':'0.014','sql':'SELECT COUNT(*)FROM`entry`'}]
创建页面已经生成了一个查询,用于计算查询中有多少条目。条目尚未提取。
将页面的对象分配给变量对象:
>>> objects = page.object_list
>>> print connection.queries
[{'time':'0.014','sql':'SELECT COUNT(*)FROM`entry`'}]
这仍然没有导致条目被提取。
生成 HttpResponse
从对象列表
>>> response = HttpResponse(page.object_list)
>>> print connection.queries
[{'time':'0.014','sql':'SELECT COUNT(*)FROM`entry`'},{'time':'0.011','sql' `entry`.`id`,< snip> FROM`entry` LIMIT 10 OFFSET 30'}]
最后,条目已被提取。 p>
I read here that Django querysets are lazy, it won't be evaluated until it is actually printed. I have made a simple pagination using the django's built-in pagination. I didn't realize there were apps already such as "django-pagination", and "django-endless" which does that job for.
Anyway I wonder whether the QuerySet is still lazy when I for example do this
entries = Entry.objects.filter(...)
paginator = Paginator(entries, 10)
output = paginator.page(page)
return HttpResponse(output)
And this part is called every time I want to get whatever page I currently I want to view.
I need to know since I don't want unnecessary load to the database.
If you want to see where are occurring, import django.db.connection
and inspect queries
>>> from django.db import connection
>>> from django.core.paginator import Paginator
>>> queryset = Entry.objects.all()
Lets create the paginator, and see if any queries occur:
>>> paginator = Paginator(queryset, 10)
>>> print connection.queries
[]
None yet.
>>> page = paginator.page(4)
>>> page
<Page 4 of 788>
>>> print connection.queries
[{'time': '0.014', 'sql': 'SELECT COUNT(*) FROM `entry`'}]
Creating the page has produced one query, to count how many entries are in the queryset. The entries have not been fetched yet.
Assign the page's objects to the variable 'objects':
>>> objects = page.object_list
>>> print connection.queries
[{'time': '0.014', 'sql': 'SELECT COUNT(*) FROM `entry`'}]
This still hasn't caused the entries to be fetched.
Generate the HttpResponse
from the object list
>>> response = HttpResponse(page.object_list)
>>> print connection.queries
[{'time': '0.014', 'sql': 'SELECT COUNT(*) FROM `entry`'}, {'time': '0.011', 'sql': 'SELECT `entry`.`id`, <snip> FROM `entry` LIMIT 10 OFFSET 30'}]
Finally, the entries have been fetched.
这篇关于Django lazy QuerySet和分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!