vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.34k stars 437 forks source link

chore: Refactor async pagination #1340

Open Amr-elwetaidy opened 1 week ago

Amr-elwetaidy commented 1 week ago

Async Paginator

The evaluate AsyncGenerator causes:

django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async

This part of the code is the reason: Screenshot_1

In fact, it's unnecessary at all as django already handles the queryset and evaluates it as an AsyncGenerator once you call it with async for


Similarly with the count() and acount() methods, you can directly call them on the queryset and no need to call all() first.

Screenshot_2

vitalik commented 1 week ago

I think .all there used to foce method that does not exist in a list

SCR-20241119-jvcn
Amr-elwetaidy commented 1 week ago

I think .all there used to foce method that does not exist in a list

SCR-20241119-jvcn

I missed that one, but still why not use if not isinstance(queryset, list): ...

Amr-elwetaidy commented 1 week ago

Anyway, The important one is the async, I had to override the class to get it working.