Skip to content
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

chore: Refactor async pagination #1340

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions ninja/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from abc import ABC, abstractmethod
from functools import partial, wraps
from math import inf
from typing import Any, AsyncGenerator, Callable, List, Optional, Tuple, Type, Union
from typing import Any, Callable, List, Optional, Tuple, Type, Union

from django.db.models import QuerySet
from django.http import HttpRequest
Expand Down Expand Up @@ -53,7 +53,7 @@ def _items_count(self, queryset: QuerySet) -> int:
"""
try:
# forcing to find queryset.count instead of list.count:
return queryset.all().count()
return queryset.count()
except AttributeError:
return len(queryset)

Expand All @@ -70,7 +70,7 @@ async def apaginate_queryset(

async def _aitems_count(self, queryset: QuerySet) -> int:
try:
return await queryset.all().acount()
return await queryset.acount()
except AttributeError:
return len(queryset)

Expand Down Expand Up @@ -203,14 +203,10 @@ async def view_with_pagination(request: HttpRequest, **kwargs: Any) -> Any:
items, pagination=pagination_params, request=request, **kwargs
)

async def evaluate(results: Union[List, QuerySet]) -> AsyncGenerator:
for result in results:
yield result

if paginator.Output: # type: ignore
result[paginator.items_attribute] = [
result
async for result in evaluate(result[paginator.items_attribute])
async for result in result[paginator.items_attribute]
]
return result

Expand Down
Loading