Getting a use a thread or sync_to_async error

So Im working on a Django web chat. I just switched my db structure to be able to support groupchats. I changed the code so far and Im struggling to figure out how to fix the following error.

django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

And here's my websocket_connect from

    async def websocket_connect(self, event):
        print('connected', event)
        user = self.scope['user']
        threads = Thread.objects.filter(participant__user=user).prefetch_related()
        for thread in threads:
            chat_room = f'user_chatroom_{}'
            self.chat_room = chat_room
            await self.channel_layer.group_add(
        await self.send({
            'type': 'websocket.accept'

I am happy for every answer!

I tried to change the threads variable but I cannot change this since I need it.

Fixed it now(temporarily). It may not be the best solution but this is what helped for me.

import os

os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

Hope I could help, and correct me if there's a better solution.

Back to Top