简体   繁体   English

Django REST框架仅返回分页和总计数所需的对象

[英]Django REST Framework Return Only Objects Needed For Pagination & Total Count

Background 背景

I'm currently using a ModelViewSet controller with get_queryset defined. 我目前正在使用具有get_queryset定义的get_queryset控制器。 The query for get_queryset returns all the objects associated with a given resource. get_queryset的查询返回与给定资源关联的所有对象。 For example a list of all the users in the system. 例如,系统中所有用户的列表。 This is great for trivial usage as the full list is always returned, paginated by the default pagination method, and then returned to the requestor as expected. 这对于琐碎的用法非常有用,因为总是返回完整列表,使用默认的分页方法对它进行分页,然后按预期返回给请求者。

Reason 原因

As our dataset gets larger and larger it seems unnecessary to query all the objects for a given endpoint, load them into a list, and then paginate that list each page load. 随着我们的数据集越来越大,似乎没有必要查询给定端点的所有对象,将它们加载到列表中,然后在每次页面加载时分页该列表。

Question

Is there any functionality to only return the objects necessary for the given page the request is for? 是否有功能仅返回请求所针对的给定页面所需的对象?

For example lets say a request comes in for the first page with a page size of 25. Currently we query all the users in the database, paginate that list, and then return the first 25 objects. 例如,假设有一个针对页面大小为25的第一页的请求。当前,我们查询数据库中的所有用户,分页该列表,然后返回前25个对象。 Then another request comes in for the second page. 然后另一个请求进入第二页。 We have to query all the users again, paginate the list, and then return the second 25 objects. 我们必须再次查询所有用户,对列表进行分页,然后返回第二个25个对象。

Instead on the first request I'd like to only query for the first 25 users (using something like LIMIT and OFFSET), then provide that result along with the total count so the pagination method can provide the proper next, previous, and count properties. 相反,在第一个请求中,我只想查询前25个用户(使用LIMIT和OFFSET之类的东西),然后提供该结果以及总计数,以便分页方法可以提供适当的next,previous和count属性。 Then on the second request I'd like to use the request information to update the LIMIT and OFFSET and return the next 25. 然后,在第二个请求中,我想使用请求信息来更新LIMIT和OFFSET并返回下一个25。

Investigation 调查中

I'm aware of the ability to write a custom Pagination class but I was wondering if there was some existing process for achieving this. 我知道编写自定义分页类的能力,但我想知道是否存在一些实现此目的的过程。

Unless I misunderstood the question, it sounds like you are getting confused by queryset = User.objects.all() . 除非我误解了这个问题,否则听起来好像您对queryset = User.objects.all()感到困惑。 This line doesn't query all users from a database because query sets are lazy . 由于查询集是惰性的,因此此行不会从数据库查询所有用户。

It should already work as you are describing - get only the first page of a list from the database using LIMIT and OFFSET, and count the total amount of records using COUNT(). 它应该已经按照您的描述工作了-使用LIMIT和OFFSET仅从数据库中获取列表的第一页,并使用COUNT()计算记录的总数。

It case your rest pagination configuration isn't setup properly it's described in details here , should be pretty straight forward. 如果您的剩余分页配置未正确设置,请在此处详细说明,应该很简单。 If you pass ?page=1 param to your view it should work as expected. 如果将?page=1参数传递给视图,则它应该可以正常工作。

(To not guess what queries are being executed you can install django debug toolbar and see raw sql.) (为了不猜测正在执行什么查询,您可以安装django调试工具栏并查看原始sql。)

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

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