Refs #31224 -- Made sync_to_async() examples use thread sensitive with ORM calls.

This commit is contained in:
Mariusz Felisiak 2020-09-03 11:33:23 +02:00 committed by Carlton Gibson
parent 0b8871ab67
commit 8d59075184
1 changed files with 4 additions and 3 deletions

View File

@ -71,17 +71,18 @@ you will need to wrap it in a :func:`sync_to_async` call. For example::
from asgiref.sync import sync_to_async
results = sync_to_async(Blog.objects.get)(pk=123)
results = await sync_to_async(Blog.objects.get, thread_sensitive=True)(pk=123)
You may find it easier to move any ORM code into its own function and call that
entire function using :func:`sync_to_async`. For example::
from asgiref.sync import sync_to_async
@sync_to_async
def get_blog(pk):
def _get_blog(pk):
return Blog.objects.select_related('author').get(pk=pk)
get_blog = sync_to_async(_get_blog, thread_sensitive=True)
If you accidentally try to call a part of Django that is still synchronous-only
from an async view, you will trigger Django's
:ref:`asynchronous safety protection <async-safety>` to protect your data from