首页 > 解决方案 > 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.

我查的资料和官方文件都是这样写的。

但是为什么我会收到错误消息?

标签: djangodjango-channels

解决方案


你的问题在stackoverflow上是重复的:

那里已经解释过:Django Channels Error: you cannot use AsyncToSync in the same thread as an async event loop


推荐阅读