🔥 Unlocking ASYNC Support in Django Ninja CRUD!
![9rmYyx](https://github.com/user-attachments/assets/23ed3003-cedb-4910-9b2e-e43f7e2be00f)
This update significantly expands Django Ninja CRUD's capabilities, allowing developers to leverage asynchronous programming in their custom API views. By releasing async `APIView` support early in `v0.6.1`, developers can start experimenting and implementing async views immediately. Full async support for built-in CRUD operations is planned for `v0.7.0`! 🌞
How to define an async reusable view?
Define async reusable views the same way as synchronous ones, using `async/await` notations for the `handler` method:
python
examples/reusable_views.py
from typing import Optional, Type
from uuid import UUID
import pydantic
from django.db import models
from django.http import HttpRequest
from ninja_crud.views import APIView
class ReusableSyncReadView(APIView):
def __init__(
self,
name: Optional[str] = None,
model: Optional[Type[models.Model]] = None,
response_body: Optional[Type[pydantic.BaseModel]] = None,
) -> None:
super().__init__(
name=name,
methods=["GET"],
path="/{id}",
response_status=200,
response_body=response_body,
model=model,
)
def handler(self, request: HttpRequest, id: UUID) -> models.Model:
return self.model.objects.get(id=id)
class ReusableAsyncReadView(APIView):
def __init__(
self,
name: Optional[str] = None,
model: Optional[Type[models.Model]] = None,
response_body: Optional[Type[pydantic.BaseModel]] = None,
) -> None:
super().__init__(
name=name,
methods=["GET"],
path="/{id}",
response_status=200,
response_body=response_body,
model=model,
)
async def handler(self, request: HttpRequest, id: UUID) -> models.Model:
return await self.model.objects.aget(id=id)
What's Changed
* feat: 🌞 add `async` handler support in `APIView` by hbakri in https://github.com/hbakri/django-ninja-crud/pull/449
* docs: 📚 update `README` and documentation `guides` by hbakri in https://github.com/hbakri/django-ninja-crud/pull/451
* fix: 🐝 expose `viewsets` module in root `__init__.py` by hbakri in https://github.com/hbakri/django-ninja-crud/pull/454
**Full Changelog**: https://github.com/hbakri/django-ninja-crud/compare/v0.6.0...v0.6.1