Introduction
Async ORM operations let your Django app handle database tasks without waiting, so it can do other things at the same time. This helps your app stay fast and responsive.
Jump into concepts and practice - no test required
Async ORM operations let your Django app handle database tasks without waiting, so it can do other things at the same time. This helps your app stay fast and responsive.
await Model.objects.afilter(field=value).afirst() await Model.objects.acreate(field=value) await Model.objects.aupdate_or_create(defaults={...}, field=value) await Model.objects.afilter(field=value).adelete()
afirst(), acreate(), aupdate_or_create(), and adelete().await to get the result.async def get_user(): user = await User.objects.afilter().afirst() return user
async def create_post(): post = await Post.objects.acreate(title='Hello', content='World') return post
async def update_or_create_profile(): profile, created = await Profile.objects.aupdate_or_create( user_id=1, defaults={'bio': 'New bio'} ) return profile, created
This example shows how to create, fetch, update, and delete records using Django's async ORM methods inside an async function.
import asyncio from django.db import models class Book(models.Model): title = models.CharField(max_length=100) async def async_orm_demo(): # Create a book asynchronously book = await Book.objects.acreate(title='Async Django') print(f'Created book: {book.title}') # Fetch the first book asynchronously first_book = await Book.objects.afilter().afirst() print(f'First book title: {first_book.title}') # Update or create a book asynchronously updated_book, created = await Book.objects.aupdate_or_create( title='Async Django', defaults={'title': 'Async Django Updated'} ) print(f'Updated book title: {updated_book.title}, Created: {created}') # Delete the book asynchronously deleted_count, _ = await Book.objects.afilter(title='Async Django Updated').adelete() print(f'Deleted books count: {deleted_count}') asyncio.run(async_orm_demo())
Async ORM requires Django 4.1 or newer.
Always run async ORM calls inside async functions and use await.
Not all ORM methods have async versions yet; check Django docs for supported methods.
Async ORM lets you run database queries without stopping your app.
Use async ORM methods with await inside async functions.
This improves app speed and user experience when handling many tasks.
await before the query method returning an awaitable.await Model.objects.all(). objects = Model.objects.all().await() misplaces await. objects = await Model.objects.all().aget() wrongly chains .aget() after all(). objects = Model.objects.all() is synchronous.async def get_usernames():
users = await User.objects.filter(is_active=True).values_list('username', flat=True)
return list(users)get_usernames() return?is_active=True. The values_list with flat=True returns usernames as a list-like object.await fetches the filtered usernames asynchronously. Converting to list returns a list of usernames of active users.async def get_first_post():
post = Post.objects.filter(published=True).first()
return postfirst() is a synchronous method and blocks if used inside async function without await.await Post.objects.filter(...).afirst() to fetch first object asynchronously.aupdate() for asynchronous bulk updates, which must be awaited.await with aupdate(). User.objects.filter(is_active=False).update(is_active=True) is synchronous. await User.objects.filter(is_active=False).update(is_active=True) uses synchronous update() with await, which is invalid. User.objects.filter(is_active=False).aupdate(is_active=True) misses await.