Skip to content

v0.6.1

Compare
Choose a tag to compare
@hbakri hbakri released this 04 Aug 22:24
· 40 commits to main since this release
ec96280

🔥 Unlocking ASYNC Support in Django Ninja CRUD!

9rmYyx

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:

# 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 #449
  • docs: 📚 update README and documentation guides by @hbakri in #451
  • fix: 🐝 expose viewsets module in root __init__.py by @hbakri in #454

Full Changelog: v0.6.0...v0.6.1