A low-effort, fully asynchronous ORM for Django applications, powered by Tortoise ORM under the hood. (draft)
Django’s async ORM currently runs asynchronous calls in a separate thread, which introduces a performance difference compared to a fully asynchronous ORM. django-raphael provides a bridge between Django models and a fully async ORM like Tortoise ORM, enabling developers to use async database operations without redefining models and while maintaining synchronization.
# books/models.py
from django_raphael.models import RaphaelMixin
class Book(RaphaelMixin, models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
# books/views.py
async def retrieve_book(request, id: int):
book = await Book.aobjects.get(id=id)
return JsonResponse({
"id": book.id,
"title": book.title,
"author": book.author,
})
async def create_book(request):
book = await Book.aobjects.create(
title=request.body.get("title"),
author=request.body.get("author"),
)
return JsonResponse({
"id": book.id,
"title": book.title,
"author": book.author,
}, status=201)
book.aobjects
returns the Tortoise ORM model.
Tortoise ORM does its thing behind the scenes and it reminds me of the Ninja Turtles. Raphael, the hot-headed one, feels like the perfect spirit animal for async Python’s feel.