如果我在 Django 中分页之前查询所有数据库有什么用?

what's the use if I am querying all the database before pagination in Django?

按照下面的代码,我正在查询数据库,然后对查询结果进行分页。所以我的问题是,如果我们在分页之前已经为所有结果访问了数据库,那有什么用。这就像 Django 分页工作还是我假装有问题? 我得到了预期的响应,但在分页之前查询数据库时遇到了问题。

代码:

class GetAllUserStoryListViewTest(APIView,LimitOffsetPagination):

    permission_classes = (IsAuthenticated,)


    def get_object(self,user_id):
        user = User.objects.filter(id = user_id).first()
        posts = Post.objects.filter(user = user)
        return posts

    def get(self,request,user_id):
        posts = self.get_object(user_id).order_by('-post_time_stamp')

        #setting the limit of this user post pagination
        LimitOffsetPagination.default_limit = settings.PAGE_SIZE
        posts = self.paginate_queryset(posts, request)
        serializer_context = {
            'request': request,
        }
        serializer = PostListSerializer(posts,many=True,context=serializer_context)
        # return Response(serializer.data)
        return self.get_paginated_response(serializer.data)

输出:

{
    "count": 7,
    "next": "http://127.0.0.1:8000/api/list_all_story_test/27/?limit=2&offset=5",
    "previous": "http://127.0.0.1:8000/api/list_all_story_test/27/?limit=2&offset=1",
    "results": [
        {
            "id": 15,
            "file": "http://127.0.0.1:8000/media/user_27/post/IMG_20190331_144024.jpg",
            "post_info": "#cool",
            "post_time_stamp": "2020-05-10T10:21:10Z",
            "total_comment": 2,
            "liked_by": [
                "vipin"
            ],
            "user": {
                "id": 27,
                "username": "vipin",
                "email": "vipinks@xyz.edu",
                "status": true,
                "profile": {
                    "id": 24,
                    "full_name": "Vipin",
                    "mobile": 6732,
                    "background_picture": "http://127.0.0.1:8000/media/user_27/profile/pexels-photo-531880_0qSgRNx.jpeg",
                    "profile_picture": "http://127.0.0.1:8000/media/user_27/profile/IMG_20190331_144024.jpg",
                    "BioDescription": "xyz",
                    "date_of_birth": "1996-06-01",
                    "gender": "M",
                    "account_type": "Private",
                    "user": 27
                },
                "last_login": null,
                "is_superuser": false,
                "is_staff": false,
                "is_active": true,
                "date_joined": "2020-04-28T11:09:27.691478Z"
            },
            "comment_usr": [
                "xyz",
                26,
                "Cool Pic"
            ],
            "like_by_you": true
        },
        {
            "id": 13,
            "file": "http://127.0.0.1:8000/media/user_30/post/IMG_20190402_102248.jpg",
            "post_info": "#Awesome",
            "post_time_stamp": "2020-05-10T10:20:22Z",
            "total_comment": 8,
            "user": {
                "id": 30,
                "username": "xyz",
                "email": "xyz@gmail.com",
                "status": false,
                "profile": {
                    "id": 27,
                    "full_name": "XYZ",
                    "mobile": 123,
                    "background_picture": null,
                    "profile_picture": "http://127.0.0.1:8000/media/user_30/profile/demo.jpg",
                    "BioDescription": null,
                    "date_of_birth": null,
                    "gender": "F",
                    "account_type": "Private",
                    "user": 30
                },
                "last_login": null,
                "is_superuser": false,
                "is_staff": false,
                "is_active": true,
                "date_joined": "2020-04-28T11:13:58.030941Z"
            },
            "like_by_you": true
        }
    ]
}

分页用Count()方式命中数据库,对结果进行分页

并且,在每个页面中通过比

更有效的切片访问数据库

使用带有 all() 方法的查询集,并使用 Iteration 访问数据库,这将加载所有结果。

filter()all() 方法不会访问数据库。请参阅 this 以检查何时评估 QuerySet