An open API service indexing awesome lists of open source software.

https://github.com/efe/django-raphael

A low-effort, fully asynchronous ORM for Django applications, powered by Tortoise ORM under the hood.
https://github.com/efe/django-raphael

Last synced: 8 months ago
JSON representation

A low-effort, fully asynchronous ORM for Django applications, powered by Tortoise ORM under the hood.

Awesome Lists containing this project

README

          

# django-raphael

A low-effort, fully asynchronous ORM for Django applications, powered by [Tortoise ORM](https://tortoise.github.io/) under the hood. (draft)

## Why?

Django’s async ORM currently runs asynchronous calls in [a separate thread](https://docs.djangoproject.com/en/5.2/topics/async/), 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.

## Usage

```python
# 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)
```

```python
# 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.

## "raphael?"

Tortoise ORM does its thing behind the scenes and it reminds me of the Ninja Turtles. [Raphael](https://en.wikipedia.org/wiki/Raphael_(Teenage_Mutant_Ninja_Turtles)), the hot-headed one, feels like the perfect spirit animal for async Python’s feel.