【发布时间】:2023-01-27 20:33:07
【问题描述】:
所以我正在处理 Django 网络聊天。我只是切换了我的数据库结构以支持群聊。到目前为止,我更改了代码,我正在努力弄清楚如何修复以下错误。
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
这是我来自 consumers.py 的 websocket_connect
async def websocket_connect(self, event):
print('connected', event)
user = self.scope['user']
print(user.online)
threads = Thread.objects.filter(participant__user=user).prefetch_related()
for thread in threads:
chat_room = f'user_chatroom_{thread.id}'
self.chat_room = chat_room
await self.channel_layer.group_add(
chat_room,
self.channel_name
)
await self.send({
'type': 'websocket.accept'
})
我为每一个答案感到高兴!
我试图更改线程变量,但我无法更改它,因为我需要它。
【问题讨论】:
标签: python django asynchronous websocket async-await