Skip to content

Added paginate query set as qs method for limit offset pagination #9100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

saichander17
Copy link

Description

Have a method in the pagination classes which returns the queryset instead of a list so that users can use the pagination classes even better.

Current Behaviour:

We currently have paginate_queryset method which paginates and returns the exact data as a list.

Required Behaviour:

Add a new method paginate_queryset_as_qs (name can be changed) which does everything as paginate_queryset but returns a queryset instead of a list.

This is a rough PR which I'm raising without really putting any efforts just to get inputs from everyone to understand if the proposal is even valid or not. If we think it's useful and can be added to the library, I can try to go in more detail and raise a PR which does changes for all the pagination classes.

Verified

This commit was signed with the committer’s verified signature.
saichander17 Sai Chander

Verified

This commit was signed with the committer’s verified signature.
saichander17 Sai Chander
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it is the right approach for the problem you are describing. and the tests are also failing for this change proposed!

@tomchristie tomchristie closed this Oct 5, 2023
@saichander17
Copy link
Author

Hi @auvipy ,
Sorry for the delayed response. Was away for while.
I want to first understand if you all think it's a valid use case or not to have paginate_queryset_as_qs in the first place. If it's agreed it could be useful, then I can focus on changing the code and getting the test cases to pass.

UseCase I have currently:
I am creating a view which returns data of tableA with some aggregated counts for each entry of tableA aggregated using multiple joins.

Current code I have:

@action(detail=False, methods=["GET"], url_name="abc", url_path="abc")
def abc(self, request):
    ordering = request.query_params.get("ordering", "-created_ts")
    input_queryset = self.filter_class(
        request.query_params, queryset=self.get_queryset().order_by(f"{ordering}"), request=request
    ).qs
    qs = aggregate_counts(input_queryset=qs)
    page = self.paginate_queryset(qs)
    serializer = WaferBookResponseSerializer(page, many=True)

    response = self.get_paginated_response(serializer.data)
    return response

def aggregate_counts(qs):
    qs.annotate(
        count1=Count('pqrs', filter=Q(pqrs__is_automated=True, pqrs__mnops__tuv__isnull=False))
    )
    for el in qs:
        print(el.count1)
    return qs

Problem in the above code is that I want to provide already paginated queryset to aggregate_counts method because otherwise it is computed over all the records in qs which is not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants