django - Django Channels ORM 数据库查询
问题描述
我不知道我做错了什么
我无法获取数据库数据。
class AsyncChatConsumer(AsyncWebsocketConsumer):
async def receive(self, text_data):
users = await self.get_users()
for user in users:
print(user.id)
@database_sync_to_async
def get_users(self):
return User.objects.all()
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
我查的资料和官方文件都是这样写的。
但是为什么我会收到错误消息?
解决方案
你的问题在stackoverflow上是重复的:
那里已经解释过:Django Channels Error: you cannot use AsyncToSync in the same thread as an async event loop
推荐阅读
- dart - dart arg 解析器实现默认子命令
- python-3.x - 为什么计算程序时间时时间总是不同
- javascript - 使用 jquery 删除后无法更新购物车表
- javascript - Discord Bot - 将消息从一个服务器通道转发到另一个服务器通道
- flutter - 为什么在 navigator.push() 之后移动到下一个屏幕后使用提供程序类会出错?
- gcc - GCC 链接器从 7.x 更改为 8.x
- php - 搜索项目时如何读取字母之间的空格
- php - JS 名字缩写
- coq - 为什么 Coq 不允许在 Linux 和 Windows 中以 QED 结尾的定理?
- javascript - 除了第一个字段外,每个按键事件都会创建新元素