# How to use Django ORM from Twisted event loop?

**URL:** <https://forum.djangoproject.com/t/how-to-use-django-orm-from-twisted-event-loop/17293>\
**Category:** Async/Channels\
**Created:** [November 27, 2022, 6:45pm UTC](https://forum.djangoproject.com/t/how-to-use-django-orm-from-twisted-event-loop/17293 "2022-11-27T18:45:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![malibu1966](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/malibu1966/32/10764_2.png) [@malibu1966](https://forum.djangoproject.com/u/malibu1966)\
**Post date:** [November 27, 2022, 6:45pm UTC](https://forum.djangoproject.com/t/how-to-use-django-orm-from-twisted-event-loop/17293/1 "2022-11-27T18:45:19Z")

</div>

Hi there, I am attempting to use the Django ORM from within a Twisted event loop. With previous versions of Python it was simple, set up the PYTHONPATH and DJANGO\_SETTINGS\_MODULE, call django.setup() and everything worked. Now with Python 3.9, Django 4.1.3, Twisted 22.10.0 I have all kinds of threading issues.

Currently I am just trying to do this:

> from asgiref.sync import sync\_to\_async  
> from django.contrib.auth.models import User  
> async def register\_user\_client(self, username):  
> u = User.objects.get(username=username)

But this doesn’t work. I get “You cannot call this from an async context - use a thread or sync\_to\_async.”

So, fine, I try to introduce sync\_to\_async:

> from asgiref.sync import sync\_to\_async  
> from django.contrib.auth.models import User  
> async def register\_user\_client(self, username):  
> u = sync\_to\_async(User.objects.get, thread\_sensitive=True)(username=username)

This gives error “twisted/internet/defer.py:1697: builtins.RuntimeWarning: coroutine ‘SyncToAsync. **call** ’ was never awaited”

So I add an await:

> from asgiref.sync import sync\_to\_async  
> from django.contrib.auth.models import User  
> async def register\_user\_client(self, username):  
> u = await sync\_to\_async(User.objects.get, thread\_sensitive=True)(username=username)

To this I get “builtins.RuntimeError: await wasn’t used with future”

I also tried a seperate method with the sync\_to\_async decorator:

> from asgiref.sync import sync\_to\_async  
> from django.contrib.auth.models import User  
> @sync\_to\_async  
> def get\_user(self, username):  
> u = User.objects.get(username=username)  
> return u
> 
> async def register\_user\_client(self, username):  
> u = self.get\_user(username)

I get “twisted/internet/defer.py:1697: builtins.RuntimeWarning: coroutine ‘SyncToAsync. **call** ’ was never awaited”

So I try with an await:

> from asgiref.sync import sync\_to\_async  
> from django.contrib.auth.models import User  
> @sync\_to\_async  
> def get\_user(self, username):  
> u = User.objects.get(username=username)  
> return u
> 
> async def register\_user\_client(self, username):  
> u = await self.get\_user(username)

Again I get “builtins.RuntimeError: await wasn’t used with future”

I also tried an asychronous support call:

> from asgiref.sync import sync\_to\_async  
> from django.contrib.auth.models import User  
> async def register\_user\_client(self, username):  
> u = await User.objects.aget(username=username)

To this I get “builtins.RuntimeError: await wasn’t used with future”

If I take out the await:

> from asgiref.sync import sync\_to\_async  
> from django.contrib.auth.models import User  
> async def register\_user\_client(self, username):  
> u = User.objects.aget(username=username)

I get “twisted/internet/defer.py:1697: builtins.RuntimeWarning: coroutine ‘QuerySet.aget’ was never awaited”

I have also tried these combinations without an ‘async’ method with similar results, and of course you can’t use an await without async.

Clearly I have been working at this for a long time. Can anyone please tell me what I’m doing wrong? I’m really hoping the answer is not that it is impossible to use Django ORM from within a Twisted run loop.

Thank you.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 27, 2022, 7:00pm UTC](https://forum.djangoproject.com/t/how-to-use-django-orm-from-twisted-event-loop/17293/2 "2022-11-27T19:00:45Z")

</div>

I don’t have an answer to your specific question.

However, I’m fairly confident that it can be done because Channels appears to use twisted in Daphne. (See [daphne/server.py at main · django/daphne · GitHub](https://github.com/django/daphne/blob/main/daphne/server.py))

You might want to take a detailed look at how Daphne works and see if its logic can be adapted to your needs.

---

<div class="post-metadata">

**Author:** ![malibu1966](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/malibu1966/32/10764_2.png) [@malibu1966](https://forum.djangoproject.com/u/malibu1966)\
**Post date:** [November 28, 2022, 1:39pm UTC](https://forum.djangoproject.com/t/how-to-use-django-orm-from-twisted-event-loop/17293/3 "2022-11-28T13:39:31Z")

</div>

I was able to figure out a solution to this, but it is ugly. If anyone has a better, more elegant solution please let me know. The following works:

> ```
> from twisted.internet import threads, defer
> from django.contrib.auth.models import User
> 
> def _get_user(self, username):
> u = User.objects.get(username=username)
> return u
> 
> @defer.inlineCallbacks
> def get_user(self, username):
> u = yield threads.deferToThread(self._get_user,username)
> print("USER:",u)
> 
> ```

This has a massive downside, however. You have to make sure the \_get\_user method returns actual result data and not a queryset because once you are out of the thread you can’t access the database for more data. In other words you need to eager load everything.

As I said, if anyone has a more elegant way to do this please let me know.
