'Modern best approach to using Django ORM with async?
The world of async Django is changing rapidly, and it's hard to tell what is current and what is dated.
So, what is the current best approach to using the Django ORM (or, possibly, another ORM) for the best/smoothest async capability? What do people use successfully today?
Lots of references out there, including:
- https://forum.djangoproject.com/t/asynchronous-orm/5925/70
- https://channels.readthedocs.io/en/latest/topics/databases.html
One problem I have is that no matter what I try, I run into the "django.db.utils.OperationalError: database "test_djangoasyncproj" is being accessed by other users" issue.
Solution 1:[1]
i think u should follow documentation https://docs.djangoproject.com/en/4.0/topics/async/#asgiref.sync.sync_to_async https://docs.djangoproject.com/en/4.0/topics/async/#async-views
from asgiref.sync import sync_to_async
results = await sync_to_async(Blog.objects.get, thread_sensitive=True)(pk=123)
or
from asgiref.sync import sync_to_async
def _get_blog(pk):
return Blog.objects.select_related('author').get(pk=pk)
get_blog = sync_to_async(_get_blog, thread_sensitive=True)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kjaw |
