0

I don't know what I did wrong

I can't get Database Data.

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.

The information I checked and the official documents are all written in this way.

But why am I getting an error?

1 Answer 1

0

Your problem is duplicate on stackoverflow:

It's already explained there: Django Channels Error: you cannot use AsyncToSync in the same thread as an async event loop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.