Skip to main content

Async Django ORM (Django Async Backend)

CI status Latest Version in PyPI Supported Python versions

Monthly downloads

Async Django ORM and PostgreSQL database backend.

📖 Read the documentation


Installation

pip install django-async-backend[binary]

The binary extra installs the C-accelerated psycopg implementation. Without it you get the pure-Python implementation, which is noticeably slower. If you use connection pooling, add the pool extra as well:

pip install django-async-backend[binary,pool]

The package tracks Django's major and minor version — for example 6.0.x matches Django 6.0 — because a large part of the ORM layer is generated from Django's own source.

Quick start

# settings.py
DATABASES = {
    "default": {
        "ENGINE": "django_async_backend.db.backends.postgresql",
        ...
    },
}

INSTALLED_APPS = [
    ...
    "django_async_backend",
]

# Required under ASGI so connections are released each request.
MIDDLEWARE = [
    "django_async_backend.middleware.close_async_connections",
    ...
]
from django.db import models
from django_async_backend.db import async_connections
from django_async_backend.db.models.base import AsyncModelMixin
from django_async_backend.db.transaction import async_atomic


class Book(AsyncModelMixin, models.Model):
    name = models.CharField(max_length=100)


async def notify(book_id: int) -> None:
    ...


async def main() -> None:
    connection = async_connections["default"]

    async with async_atomic():
        book = await Book.async_objects.acreate(name="Django")

        # async callbacks are supported; runs only if the transaction commits
        await connection.on_commit(lambda: notify(book.pk))

        book.name = "Django Async"
        await book.async_save(update_fields=["name"])

        async with async_atomic():  # savepoint
            await Book.async_objects.filter(name="draft").adelete()

    print(await Book.async_objects.acount())

    async for row in Book.async_objects.order_by("name"):
        print(row.pk, row.name)

Or drop to a raw async cursor:

async with await connection.cursor() as cursor:
    await cursor.execute("SELECT id, name FROM app_book ORDER BY name")

    print(cursor.rowcount)

    async for row in cursor:
        print(row)

[!WARNING] Async does not mean parallel. Within one async context, all ORM and cursor calls share a single connection per database alias, so queries are serialized even under asyncio.gather(). This mirrors Django's DEP 0009.

Supported methods

Legend: ✅ supported · ❌ not supported · ⚠️ supported with caveats

QuerySet methods

methods supported comments
Model.objects.aget
Model.objects.acreate
Model.objects.acount
Model.objects.none
Model.objects.abulk_create
Model.objects.abulk_update
Model.objects.aget_or_create
Model.objects.aupdate_or_create
Model.objects.aearliest
Model.objects.alatest
Model.objects.afirst
Model.objects.alast
Model.objects.ain_bulk
Model.objects.adelete
Model.objects.aupdate
Model.objects.aexists
Model.objects.acontains
Model.objects.aexplain
Model.objects.araw
Model.objects.all
Model.objects.filter
Model.objects.exclude
Model.objects.complex_filter
Model.objects.union
Model.objects.intersection
Model.objects.difference
Model.objects.select_related
Model.objects.select_for_update
Model.objects.prefetch_related
Model.objects.aaggregate
Model.objects.annotate
Model.objects.order_by
Model.objects.distinct
Model.objects.extra
Model.objects.reverse
Model.objects.defer ⚠️ not safe for async, will not be implemented — use values/values_list
Model.objects.only ⚠️ not safe for async, will not be implemented — use values/values_list
Model.objects.using
Model.objects.resolve_expression
Model.objects.ordered
Model.objects.values
Model.objects.values_list
Model.objects.dates
Model.objects.datetimes
Model.objects.alias
Model.objects.aiterator

Dunder methods

methods supported comments
__aiter__
__iter__ ⚠️ raises TypeError — use async for obj in qs
__len__ ⚠️ raises TypeError — use await qs.acount()
__contains__ ⚠️ falls back to __iter__, so it raises TypeError too
__bool__ ⚠️ truth-testing falls back to __len__, so if qs: raises TypeError — use await qs.aexists()
__repr__
__and__
__or__
__xor__
__getitem__

Model methods

methods supported comments
Model.asave async_save
Model.adelete async_delete
Model.arefresh_from_db

RawQuerySet

Not supported ❌

Related managers

Not supported ❌ — instance.<related>.all() is the sync ORM. See Pitfalls.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_async_backend-6.0.9.tar.gz (115.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_async_backend-6.0.9-py3-none-any.whl (125.3 kB view details)

Uploaded Python 3

File details

Details for the file django_async_backend-6.0.9.tar.gz.

File metadata

  • Download URL: django_async_backend-6.0.9.tar.gz
  • Upload date:
  • Size: 115.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.13 Linux/6.17.0-1020-azure

File hashes

Hashes for django_async_backend-6.0.9.tar.gz
Algorithm Hash digest
SHA256 6aed9ef7881f705b080c996d4b7942c7d42004664ae74702f05f86facb069e9b
MD5 9a31308b05efd427345e4b188bc18c02
BLAKE2b-256 3915808db2d32a17660623566f4fb7ccb109b02ae9522f50ef9b015af04bf182

See more details on using hashes here.

File details

Details for the file django_async_backend-6.0.9-py3-none-any.whl.

File metadata

  • Download URL: django_async_backend-6.0.9-py3-none-any.whl
  • Upload date:
  • Size: 125.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.13 Linux/6.17.0-1020-azure

File hashes

Hashes for django_async_backend-6.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 3cb220575bcfec6a7b1a2c221ae6a45fae947984161d074f7be9dc59956f4e4f
MD5 7d07e382a7a199632f8b1732e891e7b7
BLAKE2b-256 e449923cb1630da62f8e2dc59e65ca6b0e5f24e034b7d2b0de2dfb63e1fc8706

See more details on using hashes here.

Release history Release notifications | RSS feed

6.1.3

2 files

6.1.2

2 files

6.1.1

2 files

6.1.0

2 files

This release

6.0.9 This release

2 files

6.0.8

2 files

6.0.7

2 files

6.0.6

2 files

6.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page