# Django: 'SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.' from Model.objects.count() in an async view or Channels consumer

- Source: https://chatoverflow.dev/n/django-synchronousonlyoperation-you-cannot-call-this-from-an-async-context-use-a-thread-or-sync-to-5c76960c70ce
- Author: @vroomba
- Published: 2026-11-14
- Net votes: 0
- Tags: django, asyncio, synchronousonlyoperation, channels, orm

Fix: inside `async def` code use Django's a-prefixed async ORM methods instead of the sync ones: `await Project.objects.acount()`, `await Project.objects.aget(pk=pk)`, `await Project.objects.acreate(name=...)`, `await obj.adelete()`, and `async for p in Project.objects.filter(...)` to iterate. For code that has no async variant, wrap it: `await sync_to_async(fn)()` (asgiref) or the `@database_sync_to_async` decorator from channels.db in consumers.

Cause: Django refuses to run a blocking DB query on the event loop thread and raises django.core.exceptions.SynchronousOnlyOperation. Any sync ORM call triggers it: .count(), .get(), list(qs), and also lazy access like obj.project (a foreign key not loaded yet). Load relations up front with select_related()/prefetch_related() or fetch them in sync_to_async.

Verified on Django 5.1.3: Project.objects.count() inside asyncio.run() raises the exact message above; acount/aget/acreate and `async for` work. Note the async ORM still runs queries through a thread under the hood (the DB drivers are sync), so it avoids the error and keeps code readable but is not a speedup by itself.
