Django REST框架仅返回分页&所需的对象总数 [英] Django REST Framework Return Only Objects Needed For Pagination & Total Count

查看:308
本文介绍了Django REST框架仅返回分页&所需的对象总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ModelViewSet控制器,定义了 get_queryset get_queryset 的查询返回与给定资源关联的所有对象。例如,系统中所有用户的列表。这对于琐碎的用法非常有用,因为总是返回完整列表,使用默认的分页方法对它进行分页,然后按预期返回给请求者。

I'm currently using a ModelViewSet controller with get_queryset defined. The query for get_queryset returns all the objects associated with a given resource. 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.

随着我们的数据集越来越大,似乎没有必要查询给定端点的所有对象,将它们加载到列表中,然后在每个页面加载时对该列表进行分页。

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.

有没有功能仅返回请求所给定页面所需的对象?

Is there any functionality to only return the objects necessary for the given page the request is for?

例如,假设有一个请求请求进入页面大小为25的第一页。当前,我们查询数据库中的所有用户,分页该列表,然后返回前25个对象。然后另一个请求进入第二页。我们必须再次查询所有用户,对列表进行分页,然后返回第二个25个对象。

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. 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个用户(使用LIMIT和OFFSET之类的东西),然后提供该结果以及总数,以便分页方法可以提供适当的next,previous和count属性。然后,在第二个请求上,我想使用请求信息来更新LIMIT和OFFSET并返回下一个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. Then on the second request I'd like to use the request information to update the LIMIT and OFFSET and return the next 25.

我知道能够编写自定义分页类,但是我想知道是否存在一些实现此目标的过程。

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.

推荐答案

除非我误解了这个问题,否则听起来好像您对 queryset = User.objects.all()感到困惑。此行不会从数据库查询所有用户,因为查询集是懒惰的

Unless I misunderstood the question, it sounds like you are getting confused by queryset = User.objects.all(). This line doesn't query all users from a database because query sets are lazy.

它应该已经按照您的描述工作了-使用LIMIT和OFFSET仅从数据库中获取列表的第一页,并使用COUNT( )。

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().

如果未正确设置您的分页配置,请参见此处应该很简单。如果您将?page = 1 参数传递给视图,则它应该会按预期工作。

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.

(不要猜测是什么执行查询时,您可以安装 django调试工具栏,并查看原始sql。 )

(To not guess what queries are being executed you can install django debug toolbar and see raw sql.)

这篇关于Django REST框架仅返回分页&所需的对象总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆