A
A
Andrei1penguin12021-07-21 13:00:49
Django
Andrei1penguin1, 2021-07-21 13:00:49

Why doesn't database_sync_to_async work in Django?

Good day, you need to get access to the desired session in the connect () method of the consumer, so far there is something like this code:

async def connect(self):
        session = await database_sync_to_async(SessionStore(session_key = self.scope["user"].session_key).values())()

        ...
        await self.accept()

And throws this error:
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
You can still get the session in a simple way (apparently because the function itself is asynchronous):
session = SessionStore(session_key = self.scope["user"].session_key)

But further
session.values()
Throws all the same error

Tell me please, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question