Since 3.1 (currently beta) Django have support for async views
async def myview(request):
users = User.objects.all()
This example will not work - since ORM is not yet async ready
so what's the current workaround ?
you cannot just use sync_to_async with queryset - as they it is not evaluated:
from asgiref.sync import sync_to_async
async def myview(request):
users = await sync_to_async(User.objects.all)()
so the only way is evaluate queryset inside sync_to_async:
async def myview(request):
users = await sync_to_async(lambda: list(User.objects.all()))()
which looks very ugly
any thoughts on how to make it nicer ?